Last active
August 29, 2015 14:06
-
-
Save DamianMullins/8e28c5c8e63d4208e69c to your computer and use it in GitHub Desktop.
A Pen by Captain Anonymous.
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
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). |
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
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