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
fn.myBind(thisArg, ...preset)returns a function; calling it runsfnwiththis = thisArgand[...preset, ...callArgs].- Throws
TypeErrorif called on a non-function. new boundFn()must create an instance of the original function: ignorethisArg, keep the preset arguments, and makeinstanceofwork.- Bound
thiscannot be overridden later withcall/apply. - Nice to have:
nameis"bound " + fn.name, andlengthismax(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
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");56 function bound(...args) {7 if (new.target) return Reflect.construct(target, [...preset, ...args], new.target); // `new` ignores thisArg8 return target.apply(thisArg, [...preset, ...args]);9 }1011 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 up14 return bound;15 },16 writable: true,17 configurable: true,18 enumerable: false,19});2021const user = { name: "Ada" };22function greet(greeting, punctuation) {23 return `${greeting}, ${this.name}${punctuation}`;24}2526const greetAda = greet.myBind(user, "Hello");27console.log(greetAda("!")); // Hello, Ada!28console.log(greetAda.call({ name: "Linus" }, "?")); // Hello, Ada? - bound `this` wins29console.log(greetAda.name, greetAda.length); // "bound greet" 13031// `new` ignores the bound `this` but keeps preset arguments32function 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 } true3940// Matches the native behaviour41const native = Point.bind(null, 5);42console.log(new native(10) instanceof Point, JSON.stringify(new native(10)) === JSON.stringify(p)); // true true4344delete 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.