Skip to content

Instantly share code, notes, and snippets.

@mjc-gh
Created September 22, 2011 22: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 mjc-gh/1236236 to your computer and use it in GitHub Desktop.
Save mjc-gh/1236236 to your computer and use it in GitHub Desktop.
Javascript Object#try
// 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