Created
September 22, 2011 22:34
-
-
Save mjc-gh/1236236 to your computer and use it in GitHub Desktop.
Javascript Object#try
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
// attempts to call a function; returns undef if function is not defined | |
Object.prototype.try = function(name, args){ | |
return this[name] ? this[name].apply(this, args) : undefined; | |
}; | |
// more robust version | |
Object.prototype.try = function(name){ | |
return this[name] ? this[name].apply(this, Array.prototype.slice.call(arguments, 1)) : undefined; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment