Skip to content

Instantly share code, notes, and snippets.

@Couto
Created December 3, 2013 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Couto/7768135 to your computer and use it in GitHub Desktop.
Save Couto/7768135 to your computer and use it in GitHub Desktop.
Simplest bind function
/**
* bind
* Takes a function and returns a new one that will always have a particular context.
* If arguments are given, curry will happen: http://ejohn.org/blog/partial-functions-in-javascript/
*
* @param {Function} fn Function whose context will be changed
* @param {Object} [ctx=this] the obejct to which the context will be set
* @param {Mixed} [args...] Arguments to be passed to the resulting function
* @returns {Function}
*/
var bind = function (fn, ctx /*,args*/) {
var args = [].slice.call(arguments, 2);
return function () {
return fn.apply(ctx || this, args.concat([].slice.call(arguments, 0)));
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment