const wizard = {
name: "Merlin",
health: 50,
heal(num1, num2) {
return (this.health += num1 + num2);
},
};
const archer = {
name: "Robin Hood",
health: 5,
};
const healarcher = wizard.heal.bind(archer, 50, 30);
console.log(healarcher())
85
- bind() return a function. So, It should be called like
healarcher()
.
call() and apply() are useful for borrowing methods from an object while bind() is useful for us to call functions later on with a certain context or certain this
keyword.
bind() with function currying
function multiply(a, b) {
return a * b;
}
let multiplyByTwo = multiply.bind(this, 2);
console.log(multiplyByTwo(3));
6