Implement Function.prototype.bind

A bind polyfill returns a new function that calls the original with a fixed this and pre-filled arguments; a complete version also handles new (where the bound this is ignored), keeps the prototype chain for constructors, and sets name and length.

Requirements

  1. fn.myBind(thisArg, ...preset) returns a function; calling it runs fn with this = thisArg and [...preset, ...callArgs].
  2. Throws TypeError if called on a non-function.
  3. new boundFn() must create an instance of the original function: ignore thisArg, keep the preset arguments, and make instanceof work.
  4. Bound this cannot be overridden later with call/apply.
  5. Nice to have: name is "bound " + fn.name, and length is max(0, fn.length - preset.length).

Detecting new

Use new.target inside the bound function (or this instanceof bound in older code). When it is set, call the original with Reflect.construct(fn, args, new.target), which also handles classes correctly.

Prototype handling

Native bound functions have no prototype property, yet new bound() produces instances of the original. Using Reflect.construct gives exactly that, so instance instanceof fn is true without copying prototypes manually.

Why interviewers ask

It tests closures, this rules, new semantics, call/apply, and attention to spec details. Mentioning new.target and Reflect.construct stands out.

Code example

js
1Object.defineProperty(Function.prototype, "myBind", {
2 value: function myBind(thisArg, ...preset) {
3 const target = this;
4 if (typeof target !== "function") throw new TypeError("Bind must be called on a function");
5
6 function bound(...args) {
7 if (new.target) return Reflect.construct(target, [...preset, ...args], new.target); // `new` ignores thisArg
8 return target.apply(thisArg, [...preset, ...args]);
9 }
10
11 Object.defineProperty(bound, "name", { value: `bound ${target.name}` });
12 Object.defineProperty(bound, "length", { value: Math.max(0, target.length - preset.length) });
13 bound.prototype = target.prototype; // so `new.target.prototype` and instanceof line up
14 return bound;
15 },
16 writable: true,
17 configurable: true,
18 enumerable: false,
19});
20
21const user = { name: "Ada" };
22function greet(greeting, punctuation) {
23 return `${greeting}, ${this.name}${punctuation}`;
24}
25
26const greetAda = greet.myBind(user, "Hello");
27console.log(greetAda("!")); // Hello, Ada!
28console.log(greetAda.call({ name: "Linus" }, "?")); // Hello, Ada? - bound `this` wins
29console.log(greetAda.name, greetAda.length); // "bound greet" 1
30
31// `new` ignores the bound `this` but keeps preset arguments
32function Point(x, y) {
33 this.x = x;
34 this.y = y;
35}
36const PointAtX5 = Point.myBind({ ignored: true }, 5);
37const p = new PointAtX5(10);
38console.log(p, p instanceof Point); // Point { x: 5, y: 10 } true
39
40// Matches the native behaviour
41const native = Point.bind(null, 5);
42console.log(new native(10) instanceof Point, JSON.stringify(new native(10)) === JSON.stringify(p)); // true true
43
44delete Function.prototype.myBind;

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.

Open in study app