Created
May 15, 2012 18:00
-
-
Save kflorence/2703757 to your computer and use it in GitHub Desktop.
Call a function only if it is defined
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
| 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