Skip to content

Instantly share code, notes, and snippets.

@kflorence
Created May 15, 2012 18:00
Show Gist options
  • Select an option

  • Save kflorence/2703757 to your computer and use it in GitHub Desktop.

Select an option

Save kflorence/2703757 to your computer and use it in GitHub Desktop.
Call a function only if it is defined
var exec = (function(global) {
return function(name, args, context) {
var func = (context = context || global)[name];
return (typeof func != 'undefined') && func.apply(context, args);
};
})(this);
// Use like:
var timesTwo = function(x) {
return x * 2;
};
exec('timesTwo', [2]); // => 4
exec('nonExistantFunc'); // => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment