function a() {
console.log('hi')
}
//a()
a.call()
const wizard = {
name: 'Merlin'
health: 50,
heal() {
return this.health = 100;
}
}
const archer = {
name: 'Merlin'
health: 30,
}
wizard.heal.call(archer)
첫번째 파라미터에 함수를 어떤 객체에 붙이고 싶은지 넣어준다.
call a method of an object, substituting another object for the current object
이외 파라미터: 메소드에 넣어줄 값
const wizard = {
name: 'Merlin'
health: 50,
heal(num1, num2) {
return this.health += num1 + num2;
}
}
const archer = {
name: 'Merlin'
health: 30,
}
wizard.heal.call(archer, 40, 80)
const wizard = {
name: 'Merlin'
health: 50,
heal(num1, num2) {
return this.health += num1 + num2;
}
}
const archer = {
name: 'Merlin'
health: 30,
}
wizard.heal.apply(archer, [40, 80])
const wizard = {
name: 'Merlin'
health: 50,
heal(num1, num2) {
return this.health += num1 + num2;
}
}
const archer = {
name: 'Merlin'
health: 30,
}
const healArcher = wizard.heal.bind(archer, 40, 80)
healArcher()
// 150
function multiply(a,b) {
return a*b
}
let multiplyByTwo = multiply.bind(this, 2)
//this === Window
cosnsole.log(multiplyByTwo)
//[Function]
cosnsole.log(multiplyByTwo(4))
//8