Implement Function.prototype.bind From Scratch in JavaScript
Implement Function.prototype.bind from scratch in JavaScript, with partial arguments, new handling, length and name, and see what the polyfill cannot match.

bind returns a new function with this fixed and, optionally, some leading arguments fixed too. Writing it yourself is a classic question because it touches this, closures, apply, rest and spread, and, if the interviewer pushes, new. The short answer fits in five lines, and the full answer has a surprising amount of detail: a bound function can still be used as a constructor, in which case the bound this is ignored. This post builds bind in four steps on Node.js 24, shows what the real one does that a simple version does not, and lists the tests. Samples run as ES modules.
What bind does
fn.bind(thisArg, ...fixed) returns a bound function bf. When you call bf(...rest), the original fn runs with this equal to thisArg and the arguments [...fixed, ...rest]. Calling bf again with a different receiver, as in other.method = bf; other.method(), does not change this: it is fixed for good.
Two other properties are worth knowing before you write anything. The bound function's name is "bound " followed by the original name, and its length is the original length minus the number of fixed arguments, but not less than zero. And if you call new bf(), the bound thisArg is ignored, and the new object is created from the original function, as if bind was not there.
The first version
Function.prototype.myBind = function (thisArg, ...fixed) {
const fn = this;
return function (...rest) {
return fn.apply(thisArg, [...fixed, ...rest]);
};
};
const user = {
name: "Ana",
greet(greeting, punctuation) {
return `${greeting}, ${this.name}${punctuation}`;
},
};
const detached = user.greet;
const bound = user.greet.myBind(user, "Hello");
console.log(bound("!"));
try {
detached("Hi", "?");
} catch (error) {
console.log(error.name);
}Hello, Ana!
TypeErrorInside myBind, this is the function the method was called on, so the code stores it as fn. The returned function calls it with apply, always passing thisArg. The detached call fails because a method in a module is strict and this is undefined, so this.name throws a TypeError. The bound call works whatever the call site looks like.
Call out one point: fn must be a function. The real bind throws a TypeError when called on something else, and a good answer adds the check.
Function.prototype.myBind = function (thisArg, ...fixed) {
if (typeof this !== "function") {
throw new TypeError("Bind must be called on a function");
}
const fn = this;
return function (...rest) {
return fn.apply(thisArg, [...fixed, ...rest]);
};
};
try {
Function.prototype.myBind.call({}, null);
} catch (error) {
console.log(error.message);
}Bind must be called on a functionArguments are appended, not merged
The fixed arguments come first and the later ones follow. The returned function does not know the original's parameter count, so passing too many or too few is the original function's problem.
function describe(a, b, c) {
return [a, b, c].join("-");
}
const partial = describe.myBind(null, "x");
console.log(partial("y", "z"));
console.log(partial("y"));
console.log(partial("y", "z", "extra"));x-y-z
x-y-
x-y-zWith one argument missing, the third is undefined, and join turns an undefined element into an empty string. The extra argument is ignored by describe, since it only reads three.
Making it work with new
This is the part that separates a good answer from a very good one. When a bound function is called with new, the bound this must be ignored and a new object must be created from the prototype of the original function.
function Point(x, y) {
this.x = x;
this.y = y;
}
const NativeBound = Point.bind({ ignored: true }, 1);
const viaNative = new NativeBound(2);
console.log(viaNative.x, viaNative.y, viaNative instanceof Point, viaNative.ignored);1 2 true undefinedWith the real bind, new NativeBound(2) builds a Point with x = 1 and y = 2, the object is an instance of Point, and the ignored property from the bound this is not there. Our simple version gets this wrong.
const SimpleBound = Point.myBind({ ignored: true }, 1);
const viaSimple = new SimpleBound(2);
console.log(viaSimple.x, viaSimple instanceof Point);undefined falsenew SimpleBound(2) creates a new object, but fn.apply(thisArg, ...) writes x and y onto thisArg, the { ignored: true } object, and the function returns undefined, so the new expression yields the empty object it created itself, which is not a Point. Both the property values and the prototype are wrong.
The fix is to detect that the call is a construct call. Inside the returned function, this instanceof bound is true when it is called with new, and then the original must run with the new object as this. Also the prototype of the bound function must link to the original's prototype so that instanceof Point works.
Function.prototype.myBind = function (thisArg, ...fixed) {
if (typeof this !== "function") {
throw new TypeError("Bind must be called on a function");
}
const fn = this;
function bound(...rest) {
const isConstructCall = this instanceof bound;
return fn.apply(isConstructCall ? this : thisArg, [...fixed, ...rest]);
}
if (fn.prototype) {
bound.prototype = Object.create(fn.prototype);
}
return bound;
};
const FixedBound = Point.myBind({ ignored: true }, 1);
const made = new FixedBound(2);
console.log(made.x, made.y, made instanceof Point, made.ignored);
const callBound = Point.myBind({ label: "t" }, 5);
const target = { label: "t" };
const callResult = callBound.call(target, 6);
console.log(callResult);1 2 true undefined
undefinedWhen called with new, this is an object created from bound.prototype, which inherits from Point.prototype, so this instanceof bound is true and the original runs with that new object. The properties land on it, and made instanceof Point is true because the chain goes through Point.prototype. A plain call, such as callBound.call(target, 6), uses thisArg, and Point returns nothing, so the result is undefined.
An interviewer may ask why bound.prototype = Object.create(fn.prototype) and not bound.prototype = fn.prototype. Sharing the same object would let a later change to bound.prototype modify the original's prototype. The real bound functions have no prototype property at all, and new on them goes straight to the target function. The version here is the classic polyfill approximation.
length and name
The real bind sets the properties length and name. They are configurable, so Object.defineProperty can set them.
Function.prototype.myBind = function (thisArg, ...fixed) {
if (typeof this !== "function") {
throw new TypeError("Bind must be called on a function");
}
const fn = this;
function bound(...rest) {
return fn.apply(this instanceof bound ? this : thisArg, [...fixed, ...rest]);
}
if (fn.prototype) {
bound.prototype = Object.create(fn.prototype);
}
Object.defineProperty(bound, "length", { value: Math.max(0, fn.length - fixed.length), configurable: true });
Object.defineProperty(bound, "name", { value: `bound ${fn.name}`, configurable: true });
return bound;
};
function volume(w, h, d) {
return w * h * d;
}
const fixedWidth = volume.myBind(null, 2);
const nativeFixedWidth = volume.bind(null, 2);
console.log(fixedWidth.length, fixedWidth.name);
console.log(nativeFixedWidth.length, nativeFixedWidth.name);
console.log(fixedWidth(3, 4));2 bound volume
2 bound volume
24Both versions report a length of 2, from 3 - 1, and the name bound volume. The calculation is 2 * 3 * 4 = 24. The real bind also handles a length that is not a number and a name that is not a string, with fallbacks to zero and the empty string.
Binding twice
A bound function cannot be rebound to a different this. The first thisArg wins, because the original function is always called with it.
function whoami() {
return this.id;
}
const first = whoami.myBind({ id: "first" });
const second = first.myBind({ id: "second" });
console.log(second());firstThe second bind wraps first, and calling second() calls first.apply({ id: "second" }), but first ignores its this and uses { id: "first" }. The same is true of call and apply on a bound function.
How to present it
Start with the contract in two sentences, then write the first version in under a minute. It shows you know the mechanism: a closure that holds the target function, the receiver and the fixed arguments, and an apply that combines them. Then say what is missing, before you are asked. The three gaps are the check that the target is a function, the new path, and the length and name properties. Handling them one at a time, with a short test for each, is better than writing the complete polyfill in one go and hoping it is right.
If you have time, mention a design detail. The code stores fixed as a rest array and copies it into a new array on every call, so a caller cannot change the fixed arguments by mutating an array they passed. The real bind copies its arguments for the same reason. And mention that bind is mostly a tool for passing methods as callbacks. Modern code often uses arrow functions or class fields for that, but bind stays useful when you hold a function you do not control and need a fixed receiver for it.
Tests
import test from "node:test";
import assert from "node:assert/strict";
test("myBind fixes this and leading arguments", () => {
function add(a, b) {
return this.base + a + b;
}
const f = add.myBind({ base: 10 }, 1);
assert.equal(f(2), 13);
assert.equal(f.call({ base: 99 }, 2), 13);
});
test("myBind works with new", () => {
function Box(v) {
this.v = v;
}
const B = Box.myBind({ no: 1 });
const box = new B(7);
assert.equal(box.v, 7);
assert.ok(box instanceof Box);
});ℹ tests 2
ℹ pass 2
ℹ fail 0The first test includes f.call({ base: 99 }, 2) to prove that the bound this cannot be replaced. The second guards the new path that the first version got wrong.
What a polyfill cannot match
Two behaviors cannot be reproduced exactly. A bound function made by the language has no prototype property and is not created with the call path of an ordinary function, so bound.hasOwnProperty("prototype") is false for the real one and true for ours. And inside the real bound function, new.target is the original function. In an interview, saying that the polyfill is an approximation, and naming these two differences, is a strong finish.


