The bind() method allows an object to borrow a method from another object.
In this example, two objects (person and member) are created, and the member object borrows the fullname method from the person object.
const person = { firstName:“John”, lastName: “Doe”, fullName: function () { return this.firstName + ” “ + this.lastName; } } const member = { firstName:“Hege”, lastName: “Nilsen”, } let fullName = person.fullName.bind(member); |
To determine which object this refers to, follow this order of precedence.
Precedence |
Object |
1 |
bind() |
2 |
apply() and call() |
3 |
Object method |
4 |
Global scope |