0

After looking at this answer I became curious if it was possible to reassign addTogether.bind so that you could call it without the context argument. Some of the different methods I've tried are below:

// these are intended as separate attempts, don't execute all of them sequentially
addTogether.bind = addTogether.bind.bind(addTogether.bind);
addTogether.bind = addTogether.bind.bind(addTogether.bind(addTogether));
addTogether.bind = addTogether.bind.bind(addTogether);
addTogether.bind = addTogether.bind(addTogether);

My intention is to allow this:

function addTogether(a,b) {
    return a+b;
}

// something here to make the following possible

console.log(typeof addTogether.bind(2)); // "function"
console.log(addTogether.bind(2)(3)); // 5
console.log(addTogether(2,3)); // 5
Community
  • 1
  • 1
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153

2 Answers2

1

No, it's not possible to omit the context argument to Function.prototype.bind. You could however do

function addTogether(a,b) {
    return a+b;
}
addTogether_bind = (Function.prototype.bind).bind(addTogether, null);
//                                                                ^^^^
console.log(typeof addTogether_bind(2)); // "function"
console.log(addTogether_bind(2)(3)); // 5
console.log(addTogether(2,3)); // 5
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Oh, duh! I forgot I should have specified two arguments to `.bind.bind()` – Patrick Roberts Jun 15 '16 at 18:00
  • You do realize you could switch `partial` to `bind` and this would still work. Doesn't that mean it _is_ possible? I would accept this if you remove the part that says it's not possible. – Patrick Roberts Jun 15 '16 at 18:02
  • @PatrickRoberts: One can rename it to `bind`, yes, but it's still a different function. I'd rather not overwrite the `.bind` property for the same reasons Madara pointed out. In fact `.partial` isn't even a method any more, I just put it there for namespacing, assigning it to a completely standalone variable would've worked as well. – Bergi Jun 15 '16 at 18:07
  • I understand that it's no longer a "method", all I asked was for a possible line of code that reassigns `addTogether.bind` in order to satisfy the test cases I provided. In addition, assigning to `addTogether.bind` does not overwrite `Function.prototype.bind`, it merely shadows it for the `Function` instance `addTogether`. I don't think that's dangerous, per se, but it does break convention, I'll admit. – Patrick Roberts Jun 15 '16 at 18:11
0

You would need to change Function.prototype, something like so:

Function.prototype.partiallyApply = function(...args) {
   return this.bind(null, ...args);
};

Then you can call

const addTwo = addTogether.partiallyApply(2);

You can also directly modify .bind() but

  1. Don't
  2. You'll need to keep the old one somewhere temporarily so that you can call it
  3. No, really. Don't.
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Haha, good points. This was just one of those "what if" kind of questions. I would never _really_ do this in production code or anything like that. But thanks for the suggestion. – Patrick Roberts Jun 15 '16 at 17:58