bind、call、apply 区别

  1. call 和 apply 都是为了解决改变 this 的指向。作用都是相同的,只是传参的方式不同。
  2. 除了第一个参数外,call 可以接收一个参数列表,apply 只接受一个参数数组
let a = {
    value: 1
}
function getValue(name, age) {
    console.log(name)
    console.log(age)
    console.log(this.value)
}
getValue.call(a, 'sx', '24')
getValue.apply(a, ['sx', '24'])

bind 和其他两个方法作用也是一致的,只是该方法会返回一个函数。
并且我们可以通过 bind 实现柯里化

猜你喜欢

转载自blog.csdn.net/LuckXinXin/article/details/107655156