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 Neo for How to execute a JavaScript function when I have its name as a string

$
0
0

Thanks for the very helpful answer. I'm using Jason Bunting's function in my projects.

I extended it to use it with an optional timeout, because the normal way to set a timeout wont work. See abhishekisnot's question

function executeFunctionByName(functionName, context, timeout /*, args */ ) {var args = Array.prototype.slice.call(arguments, 3);var namespaces = functionName.split(".");var func = namespaces.pop();for (var i = 0; i < namespaces.length; i++) {context = context[namespaces[i]];}var timeoutID = setTimeout(function(){ context[func].apply(context, args)},timeout);    return timeoutID;}var _very = {    _deeply: {        _defined: {            _function: function(num1, num2) {                console.log("Execution _very _deeply _defined _function : ", num1, num2);            }        }    }}console.log('now wait')executeFunctionByName("_very._deeply._defined._function", window, 2000, 40, 50 );

Viewing all articles
Browse latest Browse all 38

Trending Articles



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