Skip to content

Instantly share code, notes, and snippets.

@afreeland
Created August 30, 2013 15:21
Show Gist options
  • Save afreeland/6390966 to your computer and use it in GitHub Desktop.
Save afreeland/6390966 to your computer and use it in GitHub Desktop.
JavaScript: Execute function via namespace
/**
* 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