Skip to content

Instantly share code, notes, and snippets.

@CatTail
Created July 12, 2013 08:27
Show Gist options
  • Save CatTail/5982799 to your computer and use it in GitHub Desktop.
Save CatTail/5982799 to your computer and use it in GitHub Desktop.
Bind function to this OR bind this to function ?!
Function.prototype.curry = function () {
var fn = this;
var args = [].slice.call(arguments, 0);
return function () {
return fn.apply(this, args.concat([].slice.call(arguments, 0)));
};
};
var bind = function (func, thisArg) {
return function () {
return func.apply(thisArg, arguments);
};
};
var bind2 = function (func, thisArg) {
var applied = func.apply.curry(thisArg);
return function () {
applied.apply(func, arguments);
};
};
@CatTail
Copy link
Author

CatTail commented Jul 12, 2013

var add = function (x, y) { return x+y; }
add(1, 2);

equals to

add.apply.apply(add, [null, [1,2]]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment