Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is bind in JavaScript

Understanding Bind in JavaScript

If you've been programming in JavaScript for a while, you've probably come across the bind function. It's one of those JavaScript concepts that can be a bit head-scratching when you first encounter it. But don't worry! We're here to break it down for you in simple terms and with real-life examples.

The Basics of Bind

The bind method is part of the function prototype in JavaScript. In non-technical terms, it's a tool that allows us to call a function with a specified this value. If you're scratching your head wondering what this is, don't worry. this in JavaScript refers to the context in which a function is called. It's kind of like saying "this house" or "this car." It's a way of referring to the object that the function is acting upon.

var car = {
  model: 'Toyota',
  color: 'red',
  getDetails: function() {
    return this.model + " " + this.color;
  }
}

In the example above, this inside getDetails refers to the car object. So, when we call car.getDetails(), it returns "Toyota red".

What Does Bind Do?

Now, what if we want to use the getDetails function in a different context, like for a different car? That's where bind comes in handy. It allows us to take a function and bind a new this context to it.

Let's say we have another car:

var anotherCar = {
  model: 'Honda',
  color: 'blue'
}

We can use bind to get the details of anotherCar like so:

var getAnotherCarDetails = car.getDetails.bind(anotherCar);

Now, when we call getAnotherCarDetails(), it returns "Honda blue". The bind method creates a new function that, when called, has its this keyword set to the provided value, in this case, anotherCar.

When to Use Bind

bind is incredibly useful in scenarios where the context of this changes, especially in event handlers and callbacks. JavaScript's event-based nature often means that the context of this can change depending on how and where a function is called, not just how it's defined.

Suppose you have a button in your HTML:

<button id="myButton">Click me!</button>

And you want to change the text of the button when it's clicked. You might have a Button object in your JavaScript like this:

var Button = {
  content: 'Click me!',
  click: function() {
    this.content = 'You clicked!';
  }
}

If you try to use the Button.click function as an event handler like so:

document.getElementById('myButton').addEventListener('click', Button.click);

You'll find that it doesn't work as expected. This is because, inside event handlers, this is set to the object firing the event (in this case, the HTML button element), not the Button object. To fix this, you can use bind:

document.getElementById('myButton').addEventListener('click', Button.click.bind(Button));

Now, this inside Button.click will refer to Button, not the HTML button element, and the code will work as expected.

Conclusion

To wrap up, bind in JavaScript is like a bridge that helps us connect a function with an object. It ensures that the function always knows which object it should interact with, no matter how or where the function is called. It's a bit like giving a dog (our function) a scent (our object) to follow. No matter where the dog goes or who calls it, it will always be able to find its way back to the scent. So, the next time you see bind being used in JavaScript, remember our friend, the dog, and hopefully, the concept will seem a little less mysterious.