bind、call和apply方法的使用
const Obj1
= {
name
:'小明',
sayWhat(what
,when
){
console
.log(`${this.name}在${when}说${what}`)
}
}
const Obj2
= {
name
:'小红'
}
const sayWhat
= Obj1
.sayWhat
.bind(Obj2
,'你好啊','上午2点')
sayWhat()
Obj1
.sayWhat
.apply(Obj2
,['你好啊','上午2点'])
Obj1
.sayWhat
.call(Obj2
,'你好啊','上午2点')
三者区别
bind返回值为函数需要重新执行重新执行时不需要再传递参数,apply和call则为直接调用,不需要另外执行apply传递参数需要传入数组,call和bind则依次传入即可
转载请注明原文地址:https://tech.qufami.com/read-19903.html