Skip to the content

Lesson 77 of 296

Your own bind

On this page

The task

"Implement Function.prototype.bind yourself."

What the interviewer is checking

  • How this works (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

  1. Return a new function.
  2. When called, run the original with this set to the chosen object.
  3. Put any preset arguments first, then the arguments given later.
  4. If the bound function is called with new, ignore the bound this and 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, this is the function myBind was called on (a method call: rule 1).
  • We save it in originalFn, because inside the returned function this will be different.
  • The returned function is a closure over originalFn, context and presetArgs.

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 is bound.prototype, so this instanceof bound is true. Then we use that new object as this, not the bound context.
  • Setting bound.prototype to an object that inherits from originalFn.prototype means instances still get the original's methods and pass instanceof checks.
  • bound must be a normal function, not an arrow function, so it has its own this and can be used with new.

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                true

Your 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 this inside 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

WordMeaning
Bound functionA function whose this is fixed
ContextThe value used as this
Preset argumentsArguments fixed when binding

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.