Created
April 17, 2013 07:08
-
-
Save ginpei/5402338 to your computer and use it in GitHub Desktop.
Describe an immediate function to read easily.
This file contains 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
/** | |
* Runs the function immediately. | |
* @param {Array} [args] Arguments for the function. | |
* @param {Object} [context] Context for the function. | |
* @param {Function} fn Target function. | |
* @returns {Any} Value returned by the function. | |
* @example run(function(){ console.log('done!'); }); | |
* @example run([1,2], function(a,b){ return a+b; }); | |
* @example var date = Date.now(); run([Date.now()], {start:date}, function(stop){ return stop-this.start; }); | |
*/ | |
function run(args, context, fn) { | |
if (!fn) { fn = context; context = undefined; } | |
if (!fn) { fn = args; args = undefined; } | |
if (fn && fn.apply) { return fn.apply(context, args); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment