7.4. Implement Function.prototype.bind (with new)
Topics
This question is in
7. this, call, apply and bindfn.bind(thisArg, a) returns a new function with this and its first arguments fixed in advance. It is how a method keeps its object when passed as a callback (button.addEventListener("click", menu.open.bind(menu))) and how a general function becomes a specific one (const double = multiply.bind(null, 2)). The returned function is more than a wrapper: called with new, it forgets the bound this and builds an object of the original function, and its name and length follow precise rules.
Add myBind to every function, in bind.js. fn.myBind(thisArg, ...boundArgs) must behave like fn.bind(thisArg, ...boundArgs). You may use call and apply, but not bind or Reflect.construct.
Signature
fn.myBind(thisArg, ...boundArgs) // returns the bound functionthisArg: thethisfor every normal call of the bound function.boundArgs: arguments placed before the ones each call passes.- Returns a new function, the bound function. Calling it runs
fn; calling it withnewconstructsfn.
Requirements
- Every function has a
myBindmethod that does not appear in afor…inloop, with alengthof 1. It returns a new function and does not callfn. - A normal call runs
fnwiththisArgasthisandboundArgsfollowed by the call's own arguments, and returns the result. - The bound
thiscannot be changed: not by calling the bound function as a method, nor withcallorapply, nor by binding it again. thisfollows the built-in rules: a strictfnseesthisArgexactly (undefined,null,0included); a sloppyfnsees the global object fornullorundefinedand a wrapper object for a primitive.- Every call starts from the same bound arguments: calls never add to them. Bound functions made separately are independent.
- With
new, the boundthisis ignored:fnis constructed with the bound and given arguments,new.targetinsidefnisfn, and the result is an instance of bothfnand the bound function, or the objectfnreturns. This works for classes, for a function bound twice, and for a function already bound with the built-inbind:instanceofon the bound function gives the same answer as onfn. - Calling a bound class without
newthrows aTypeError, and so doesnewon a bound arrow function. lengthisfn's ownlengthminus the number of bound arguments, never below 0: a fractional length is cut to a whole number,InfinitystaysInfinity, and a length that is missing or not a number counts as 0.nameis"bound "followed byfn's name ("bound "alone when the name is empty or not a string). Both are read-only, non-enumerable and configurable, as on built-in functions.fnis not changed. CallingmyBindon something that is not a function throws aTypeError. It does not usebindorReflect.construct.
Examples
function greet(greeting, mark) {
return greeting + ", " + this.name + mark;
}
const hiAna = greet.myBind({ name: "Ana" }, "Hi");
hiAna("!"); // "Hi, Ana!"
hiAna.call({ name: "Bo" }, "?"); // "Hi, Ana?": this stays bound
hiAna.name; // "bound greet"
hiAna.length; // 1function Point(x, y) {
this.x = x;
this.y = y;
}
const OnAxis = Point.myBind({ ignored: true }, 0);
const p = new OnAxis(5); // Point { x: 0, y: 5 }
p instanceof Point; // true
p instanceof OnAxis; // trueconst add = (a, b) => a + b;
const inc = add.myBind(null, 1);
inc(41); // 42
new inc(1); // throws TypeError: arrow functions cannot be constructedEnvironment
- Plain JavaScript in a browser page;
bind.jsruns first, then the tests callmyBindon functions they define in both strict and sloppy code. - While
myBindand the functions it returns run, the tests replacebindandReflect.constructwith versions that report their use. - Out of scope: a bound function used as the parent of a
class … extends, and the fact that a real bound function has noprototypeproperty (a function you write always has one).
Hints
- Inside a function, what tells you that it was called with
new? new target(...args)builds an object exactly asnewontargetwould. What does that give you for free?- A function's
nameandlengthare read-only. How can you set them on the function you return?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it