Lesson 77 of 296
Your own bind
On this page
The task
"Implement Function.prototype.bind yourself."
What the interviewer is checking
- How
thisworks (Lesson 55). - Closures (Lesson 53).
- Rest and spread for arguments (Lesson 62).
- For senior roles: what happens when a bound function is used with
new.
What bind must do
- Return a new function.
- When called, run the original with
thisset to the chosen object. - Put any preset arguments first, then the arguments given later.
- If the bound function is called with
new, ignore the boundthisand create a new instance as normal.
Step 1: the basic version
Function.prototype.myBind = function (context, ...presetArgs) {
const originalFn = this;
return function (...laterArgs) {
return originalFn.apply(context, [...presetArgs, ...laterArgs]);
};
};- Inside
myBind,thisis the functionmyBindwas called on (a method call: rule 1). - We save it in
originalFn, because inside the returned functionthiswill be different. - The returned function is a closure over
originalFn,contextandpresetArgs.
This basic version answers most interviews. Say that you know there is a new case, then add it if they want.
Step 2: support new
Function.prototype.myBind = function (context, ...presetArgs) {
if (typeof this !== "function") {
throw new TypeError("myBind must be called on a function");
}
const originalFn = this;
function bound(...laterArgs) {
const isNew = this instanceof bound;
return originalFn.apply(isNew ? this : context, [...presetArgs, ...laterArgs]);
}
if (originalFn.prototype) {
bound.prototype = Object.create(originalFn.prototype);
}
return bound;
};- When called with
new, JavaScript creates an object whose prototype isbound.prototype, sothis instanceof boundis true. Then we use that new object asthis, not the bound context. - Setting
bound.prototypeto an object that inherits fromoriginalFn.prototypemeans instances still get the original's methods and passinstanceofchecks. boundmust be a normal function, not an arrow function, so it has its ownthisand can be used withnew.
Test it
function introduce(city, country) {
return `${this.name} lives in ${city}, ${country}`;
}
const fn = introduce.myBind({ name: "Sam" }, "Leeds");
fn("UK"); "Sam lives in Leeds, UK"
function Point(x, y) { this.x = x; this.y = y; }
Point.prototype.sum = function () { return this.x + this.y; };
const BoundPoint = Point.myBind({ ignored: true }, 2);
const p = new BoundPoint(3);
p.sum(); 5
p instanceof Point trueYour own call and apply (bonus)
Sometimes asked as a follow-up. The trick: temporarily make the function a method of the context object, call it, then remove it.
Function.prototype.myCall = function (context, ...args) {
context = context ?? globalThis;
const key = Symbol("fn");
context[key] = this;
const result = context[key](...args);
delete context[key];
return result;
};A Symbol key avoids overwriting any real property. (This simple version does not handle primitive contexts like numbers.) myApply is the same, but takes an array of arguments.
Follow-up questions
- "Can you bind a bound function again to change
this?" No; the first binding wins, because the inner call always uses the saved context. - "What does bind do to arrow functions?" Arrow functions ignore it for
this; preset arguments still work.
Common mistakes
- Using
thisinside the returned function and expecting it to be the original function. - Making the returned function an arrow function in the
new-aware version. - Forgetting the later arguments.
How to say it in an interview
"myBind captures the original function, the context and any preset arguments in a closure, and returns a new function that calls the original with apply, using the saved context and the preset arguments followed by the new ones. To support new, I check whether this is an instance of the bound function; if so I use the new object as this instead, and I link the bound function's prototype to the original's so instances inherit correctly."
Key words
| Word | Meaning |
|---|---|
| Bound function | A function whose this is fixed |
| Context | The value used as this |
| Preset arguments | Arguments fixed when binding |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.