1

If I want to partially apply a function I can use bind, but it seems I have to affect the receiver of the function (the first argument to bind). Is this correct?

I want to perform partial application using bind without affecting the receiver.

myFunction.bind(iDontWantThis, arg1); // I dont want to affect the receiver
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • 1
    You cannot. Use a different `partial` function, not `bind`. – Bergi Feb 26 '15 at 14:24
  • related, if not duplicate: [How to bind function arguments without binding this?](http://stackoverflow.com/q/13851088/1048572) – Bergi Apr 29 '15 at 12:34

1 Answers1

1

partial application using bind without affecting the receiver

That's not possible. bind was explicitly designed to partially apply the "zeroth argument" - the this value, and optionally more arguments. If you only want to fix the first (and potentially more) parameters of your function, but leave this unbound, you'll need to use a different function:

Function.prototype.partial = function() {
    if (arguments.length == 0)
        return this;
    var fn = this,
        args = Array.prototype.slice.call(arguments);
    return function() {
        return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
    };
};

Of course, such a function is also available in many libraries, e.g. Underscore, Lodash, Ramda etc. There is no native equivalent however.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375