1

I have the following code and I can get the same result without using the "bind" method.

var car = { 
registrationNumber: "GA12345",
brand: "Toyota",

displayDetails: function(ownerName){
    console.log(ownerName + ", this is your car: " + this.registrationNumber 
+ " " + this.brand);
    }
}

var myCarDetails = car.displayDetails("Vivian")
var myCarDetails = car.displayDetails.bind(car, "Vivian");

myCarDetails();

In other words, Can you show me a real life example where we need to use "bind"? I know how to use bind, I just don't know why/when I would need to use it?

Hossam Mohamed
  • 126
  • 1
  • 7
  • 1
    For example, you want to bind an event handler function to a specific object instead of the element the event was attached to. – Teemu Jun 19 '19 at 18:33
  • So I would assume that the main reason is to have a cleaner code. Using your example, instead of writing a new event handler, I would just bind to one I already used in my code and make it look cleaner? – Hossam Mohamed Jun 19 '19 at 18:44
  • I don't know if it would be more cleaner, but it could be tons of less code. – Teemu Jun 19 '19 at 18:45

0 Answers0