Skip to content

Instantly share code, notes, and snippets.

@makeusabrew
Created January 10, 2011 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makeusabrew/772540 to your computer and use it in GitHub Desktop.
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
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