Skip to content

Instantly share code, notes, and snippets.

@ginpei
Created April 17, 2013 07:08
Show Gist options
  • Save ginpei/5402338 to your computer and use it in GitHub Desktop.
Save ginpei/5402338 to your computer and use it in GitHub Desktop.
Describe an immediate function to read easily.
/**
* 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