call(), apply() and bind()
These three are the prototype functions of function.
call():
Ex:
function greet(msg) {
console.log(msg);
}
greet("hello"); // hello
greet.call(this, "hello"); // helloconst wizard = {
name: 'Merlin',
health: 100,
heal: function(num1, num2) {
this.health += num1 + num2;
}
}
const archer = {
name: 'Robin Hood',
health: 50
}
wizard.heal.call(archer, 50, 60) // 160apply():
bind():
Last updated