Skip to content

Instantly share code, notes, and snippets.

@gnclmorais
Forked from Couto/bind.js
Last active August 29, 2015 14:11
Show Gist options
  • Save gnclmorais/bd3ec4328240f724bd32 to your computer and use it in GitHub Desktop.
Save gnclmorais/bd3ec4328240f724bd32 to your computer and use it in GitHub Desktop.
/**
* 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