Created
September 20, 2013 11:57
-
-
Save mateuszkocz/6636420 to your computer and use it in GitHub Desktop.
Execute function with string. Source: http://stackoverflow.com/questions/359788/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
// Simple execution | |
window["functionName"](arguments); | |
// Nested exection. | |
window["My"]["Namespace"]["functionName"](arguments); | |
// Helper function. | |
function executeFunctionByName(functionName, context /*, args */) { | |
var args = Array.prototype.slice.call(arguments).splice(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(this, args); | |
} | |
executeFunctionByName("My.Namespace.functionName", window, arguments); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment