Created
June 16, 2017 10:18
-
-
Save ahvonenj/36b94fa5f45937b093f419eac582c420 to your computer and use it in GitHub Desktop.
How to execute a JavaScript function when I have its name as a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string | |
function Exec(functionName, context) | |
{ | |
if(typeof functionName === 'undefined') | |
{ | |
console.error('Could not execute undefined function'); | |
return function() {} | |
} | |
var args = Array.prototype.slice.call(arguments, 2); | |
var namespaces = functionName.split("."); | |
var func = namespaces.pop(); | |
for (var i = 0; i < namespaces.length; i++) | |
{ | |
context = context[namespaces[i]]; | |
} | |
return context[func].apply(context, args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment