JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

The this Keyword

javascript
senior
this

How would you implement Function.prototype.bind from scratch?

bind
polyfill
this
implementation
interview-coding
Quick Answer

A bind polyfill returns a new function that calls the original with a fixed `this` context and optionally prepended arguments. The implementation must handle partial application, preserve the prototype chain when used with `new`, and forward all arguments correctly.

Detailed Explanation

Implementing Function.prototype.bind from scratch is a common interview question at companies like Swiggy and Flipkart. It tests deep understanding of this binding, closures, argument handling, and the new operator.

What bind Does

bind(thisArg, ...args) returns a new function where:

  1. this is permanently set to thisArg
  2. Prepended arguments (args) are baked in (partial application)
  3. Additional arguments passed at call time are appended
  4. When the bound function is used with new, the thisArg is ignored and the new object becomes this

Implementation Requirements

  1. Context binding — Use apply or call to set this
  2. Partial application — Merge bound args with call-time args
  3. new operator support — Detect when the bound function is called with new and skip the forced this binding. The returned object should inherit from the original function's prototype.
  4. Prototype chain — The bound function's prototype must link to the original function's prototype so instanceof works correctly
  5. Non-function guard — Throw a TypeError if bind is called on a non-function

Key Interview Points

  • Why check this instanceof boundFn? This detects whether the bound function was called with new. When new boundFn() is used, this is a new object whose prototype is boundFn.prototype, making this instanceof boundFn true.
  • Why use Object.create for the prototype? Using boundFn.prototype = originalFn.prototype would create a shared reference — modifying the bound function's prototype would also modify the original's. Object.create creates a separate object that inherits from the original prototype.
  • What about arrow functions? Arrow functions have no this binding and no prototype property. bind returns a function, but the this override has no effect since arrow functions capture this lexically.

Code Examples

Basic bind polyfillJavaScript
Function.prototype.myBind = function (thisArg, ...boundArgs) {
  if (typeof this !== 'function') {
    throw new TypeError('myBind must be called on a function');
  }

  const originalFn = this;

  return function boundFn(...callArgs) {
    // Merge bound args with call-time args
    return originalFn.apply(thisArg, [...boundArgs, ...callArgs]);
  };
};

// Usage
const greet = function (greeting, punctuation) {
  return `${greeting}, ${this.name}${punctuation}`;
};

const greetAlice = greet.myBind({ name: 'Alice' }, 'Hello');
console.log(greetAlice('!')); // 'Hello, Alice!'
console.log(greetAlice('?')); // 'Hello, Alice?'

Real-World Applications

Use Cases

Event Handler Binding in Class Components

React class components use bind in constructors to preserve `this` context when methods are passed as event callbacks.

Partial Application for Configuration

Creating pre-configured functions using bind to fix certain parameters, such as a logger bound to a specific module name.

Mini Projects

Complete Function Methods Polyfill

advanced

Implement call, apply, and bind from scratch with full spec compliance including Symbol.hasInstance and proper length/name properties.

Industry Examples

MDN

MDN's bind polyfill page documents the full specification including edge cases with new, making it the reference implementation.

Resources

MDN - Function.prototype.bind()

docs

JavaScript.info - Function binding

article

Related Questions

What are call, apply, and bind and how do they differ?

mid
this

How does the 'this' keyword work in JavaScript?

mid
this
Previous
How do WebSockets work and when would you use them over HTTP?
Next
How would you implement JSON.stringify from scratch?
PrevNext