call(back)

Implement Function.prototype.bind

Implement Function.prototype.myBind(thisArg, ...boundArgs) without calling the native bind. It returns a new function that, when called, invokes the original with this set to thisArg and boundArgs placed before the call's own arguments.

const john = { age: 42, getAge() { return this.age; } };
const unbound = john.getAge;
unbound();                  // undefined
john.getAge.myBind(john)(); // 42
  • Nothing runs until the returned function is called.
  • Arguments given to myBind come first, then the arguments of each call.
  • Calling the bound function as a method of some other object must not change its this.

The grader runs scripted scenarios against your myBind; the native bind is switched off while they run.

Hints

Hint 1

Inside myBind, this is the function it was called on. Capture it in a variable before returning the wrapper.

Hint 2

The wrapper is function (...callArgs) { return original.apply(thisArg, [...boundArgs, ...callArgs]); }. apply is allowed; only bind is not.

Asked at

Similar Problems6