const wizard = {
name: "Merlin",
health: 50,
heal(num1, num2) {
return (this.health += num1 + num2);
},
};
const archer = {
name: "Robin Hood",
health: 5,
};
console.log("first", archer);
wizard.heal.call(archer, 50, 30);
console.log("second", archer);
first { name: 'Robin Hood', health: 5 }
second { name: 'Robin Hood', health: 85 }
apply()
The only difference between call() and apply() is that instead of call() that just takes parameters 50, 30. However, apply that takes an array of parameters [50,30].
const wizard = {
name: "Merlin",
health: 50,
heal(num1, num2) {
return (this.health += num1 + num2);
},
};
const archer = {
name: "Robin Hood",
health: 5,
};
console.log("first", archer);
wizard.heal.apply(archer, [50, 30]);
console.log("second", archer);
first { name: 'Robin Hood', health: 5 }
second { name: 'Robin Hood', health: 85 }