Skip to curriculum
Jump to the editor

7.4. Implement Function.prototype.bind (with new)

Hard
Topics
30–45 min

fn.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

JAVASCRIPT
fn.myBind(thisArg, ...boundArgs) // returns the bound function
  • thisArg: the this for 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 with new constructs fn.

Requirements

  1. Every function has a myBind method that does not appear in a for…in loop, with a length of 1. It returns a new function and does not call fn.
  2. A normal call runs fn with thisArg as this and boundArgs followed by the call's own arguments, and returns the result.
  3. The bound this cannot be changed: not by calling the bound function as a method, nor with call or apply, nor by binding it again.
  4. this follows the built-in rules: a strict fn sees thisArg exactly (undefined, null, 0 included); a sloppy fn sees the global object for null or undefined and a wrapper object for a primitive.
  5. Every call starts from the same bound arguments: calls never add to them. Bound functions made separately are independent.
  6. With new, the bound this is ignored: fn is constructed with the bound and given arguments, new.target inside fn is fn, and the result is an instance of both fn and the bound function, or the object fn returns. This works for classes, for a function bound twice, and for a function already bound with the built-in bind: instanceof on the bound function gives the same answer as on fn.
  7. Calling a bound class without new throws a TypeError, and so does new on a bound arrow function.
  8. length is fn's own length minus the number of bound arguments, never below 0: a fractional length is cut to a whole number, Infinity stays Infinity, and a length that is missing or not a number counts as 0.
  9. name is "bound " followed by fn'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.
  10. fn is not changed. Calling myBind on something that is not a function throws a TypeError. It does not use bind or Reflect.construct.

Examples

JAVASCRIPT
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;                      // 1
JAVASCRIPT
function 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;      // true
JAVASCRIPT
const add = (a, b) => a + b;
const inc = add.myBind(null, 1);
inc(41);      // 42
new inc(1);   // throws TypeError: arrow functions cannot be constructed

Environment

  • Plain JavaScript in a browser page; bind.js runs first, then the tests call myBind on functions they define in both strict and sloppy code.
  • While myBind and the functions it returns run, the tests replace bind and Reflect.construct with 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 no prototype property (a function you write always has one).

Hints

  1. Inside a function, what tells you that it was called with new?
  2. new target(...args) builds an object exactly as new on target would. What does that give you for free?
  3. A function's name and length are 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
Back to the question
Loading the editor…