Created
January 10, 2011 08:34
-
-
Save makeusabrew/772540 to your computer and use it in GitHub Desktop.
Test to see if an object can execute a dynamically passed function name
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
var MyObject = { | |
invokeMethod: function(method, args) { | |
if (typeof MyObject[method] == "function") { | |
MyObject[method](args); | |
} else { | |
throw new TypeError("MyObject has no function "+method); | |
} | |
}, | |
someMethod: function(args) { | |
// foo | |
} | |
}; | |
MyObject.invokeMethod("someMethod", {}); | |
MyObject.invokeMethod("badMethod", {}); // type error exception |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment