Created
August 30, 2013 15:21
-
-
Save afreeland/6390966 to your computer and use it in GitHub Desktop.
JavaScript: Execute function via namespace
This file contains 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
/** | |
* Executes a function by simply providing a namespace | |
* @param {string} functionName A namespace such as '_n.Roles.Order.SetOrderNotes' | |
* @param {obj} context The Initial object context to start from, such as window | |
* @return Returns back the result of whatever the function returns | |
*/ | |
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(window, args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment