Quantcast
Channel: How to execute a JavaScript function when I have its name as a string - Stack Overflow
Viewing all articles
Browse latest Browse all 38

Answer by catcher for How to execute a JavaScript function when I have its name as a string

$
0
0

I like concise solutions, so come up with this more ES6 way:

const executeByName = (name, originContext, ...args) => {  const namespaces = name.split('.');  const func = namespaces.pop();  const funcContext = namespaces.reduce((context, namespace) => context[namespace], originContext);  return funcContext[func](...args);};

Viewing all articles
Browse latest Browse all 38

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>