♥call()
用法:函数方法.call(对象/数组,参数1,参数2)
let nini ={
name:'nini',
money:50,
}
function charge(e1,e2){
return this.money + e1 + e2
}
let res = charge.call(nini,1,2)
//将函数转移到nini对象中,并让nini执行,如:nini.charge(1,2)
console.log(res) //53
♥apply
用法:函数方法.call(对象/数组,[参数1,参数2])
apply与call不同在于传参方式,apply以数组形式传参,call以单个变量传参
let nini ={
name:'nini',
money:50,
}
function charge(e1,e2){
return this.money + e1 + e2
}
let res = charge.call(nini,[1,2])
//将函数转移到nini对象中,并让nini执行,如:nini.charge(1,2)
console.log(res) //53
♥bind()
用法:函数方法.bind(对象/数组)
使用bind()返回的一个转移在目标对象的方法,执行它需要再加括号
let nini ={
name:'nini',
money:50,
}
function charge(e1,e2){
return this.money + e1 + e2
}
let res = charge.bind(nini) //将函数转移到nini对象中,返回的是一个函数
console.log(res(1,2)) //53