Blog/NotesConcept

Bind Polyfill in JavaScript: Step by Step Explanation

A concise explanation of the bind method in JavaScript, followed by a step-by-step exploration of how to create bind polyfill in JavaScript by understanding its internal implementation.

Beginner

Ram V

Last Updated Jun 10, 2025


Advertisement

Bind Polyfill in JavaScript

How does "bind" work in JavaScript?

bind method takes the first argument as an object (which works as a context) and the rest arguments are individually passed to the function. The "bind" method returns the borrowed function with bonded context and the returned function can be called

Example:

function userInfo(city) {
    console.log(`${this.name} is ${this.age} years old, lives in ${city}`);
  }

// Object Context
const user = {
    name: "Ram",
    age: 23,
};

// First Arg - Context (user)
// The rest of the arguments passed to the function, here 'Jaipur' passed to the city arg
const functionWithContext = userInfo.bind(user, 'Jaipur');

functionWithContext(); // Output: Ram is 23 years old, lives in Jaipur

Understand bind polyfill in JavaScript: Step-by-Step Explanation

Step 1: Validate, that the invoker should function, if not then throw an Error

Step 2: Set context to globalThis, if nothing is defined. globalThis has window context in the browser and global in the Node environment. It means if there is no context defined as part bind(), then the default context is the window.

Step 3: Create a symbol and assign the function to that symbol key, it works like below

let's say context (object) has a key mapped to the function, and in this case, that function can access the object context

context obj {
      context keys
      <unique symbol key> : this  ğŸ‘ˆ Now this has access to the whole context object
}

Step 4: Finally Return a function, and on call of that bonded function it calls currentContext[<unique symbol key>] with the provided rest of the arguments as part of bind and while calling that function. 

Function.prototype.myCustomBind = function (context, ...args) {
   
     // Check if the myCustomBind is invoked by a function <this>.myCustomBind()
    if (typeof this !== "function") {
      throw new Error(this + "is not a function");
    }

    // Set the context to the provided context or globalThis if none is provided
    const currentContex = context || globalThis;

    // Create a unique property to avoid name collisions
    const newFunc = Symbol();

    // Assign the original function to the new property in the context
    currentContex[newFunc] = this;

    // Return a new function that, when called, will execute the original function
    return function (...newArgs) {
          // Call the original function with the original args and any new args
          return currentContex[newFunc](...args, ...newArgs);
    };
};

const defaultBind = userType.bind(user);
defaultBind(); // Ram is 23 years old

const bindFunc = userType.myCustomBind(user);
bindFunc();  // Ram is 23 years old

Once you understand how binds work, it will be very easy to understand the other function methods call & apply and their polyfill implementation.

Further Reading

👉 Call Polyfill in JavaScript: Step by Step Explanation

 

Share this post now:

Advertisement

💬 Comments (0)

Login to comment

Advertisement

Flaunt You Expertise/Knowledge & Help your Peers

Sharing your knowledge will strengthen your expertise on topic. Consider writing a quick Blog/Notes to help frontend folks to ace Frontend Interviews.

Advertisement


Other Related Blogs

Master Hoisting in JavaScript with 5 Examples

Alok Kumar Giri

Last Updated Jun 2, 2025

Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.

20+ Frontend Machine Coding Interview Questions (JS + React)

Anuj Sharma

Last Updated Jun 24, 2025

A detailed list of 20+ most asked Frontend Machine Coding Interview Questions and resources both in JavaScript & React. Also covers expected functional/Non-functional requirements in this Interview.

Best Cheat Sheet for Frontend Machine Coding Interview Round

Anuj Sharma

Last Updated Jun 13, 2025

A comprehensive cheat sheet for the Frontend Machine Coding Interview Round, which helps to revise all the important machine coding & UI design concepts before the interview.

HTTP/2 vs HTTP/1.1: What's the Key Difference?

Anuj Sharma

Last Updated Jan 29, 2025

Understand the difference between HTTP/2 vs HTTP/1.1 based on the various parameters, which helps to understand the improvement areas of HTTP/2 over HTTP 1.1

Part 1: From Zero to Published — How I Built and Published My First React NPM Package

Akash Deep Chitransh

Last Updated May 26, 2025

Learn how to build and publish your own NPM package with Rollup, testing, and troubleshooting. Stay tuned for part 2: building a React state management library!

Understand JavaScript Date Object with Examples (for JavaScript Interviews)

Anuj Sharma

Last Updated Jan 9, 2025

Go through different ways to display dates using javascript date object. It covers examples of date object usage to understand the main concepts of javascript date object.

FrontendGeek
FrontendGeek

© 2024 FrontendGeek. All rights reserved