Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DamianMullins/8e28c5c8e63d4208e69c to your computer and use it in GitHub Desktop.
Save DamianMullins/8e28c5c8e63d4208e69c to your computer and use it in GitHub Desktop.
A Pen by Captain Anonymous.
Execute a function by specifying its name as a string.
-----
A [Pen](http://codepen.io/anon/pen/AgCDt) by [Anonasaurus Rex](http://codepen.io/anon) on [CodePen](http://codepen.io/).
[License](http://codepen.io/anon/pen/AgCDt/license).
function executeFunctionByName(functionName, context /*, args */) {
var context = context || window,
args = Array.prototype.slice.call(arguments, 2),
namespaces = functionName.split("."),
func = namespaces.pop();
for (var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
return context[func].apply(context, args);
}
var Foo = (function (window, document, undefined) {
var me = {};
me.Bar = function() {
alert('baz');
}
return me;
})(window, document);
executeFunctionByName("Foo.Bar");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment