相同点

  • 三者都是用来改变函数体内部 this 的指向;
  • 三者第一个参数都是this要指向的对象,也就是想指定的上下文;
  • 三者都可以利用后续参数传参;

    不同点

  • bind是返回对应函数,便于稍后调用;apply、call则是立即调用
  • apply、call接收参数的方式不一样
func.call(this, arg1, arg2);
func.apply(this, [arg1, arg2]);
  • bind()提供永久的绑定,还会覆盖后面的call\apply命令。使用bind()绑定后,再使用call()或者apply()来不能重新绑定this
function returnThis () {
  return this
}
var boss1 = { name: 'boss1'}
var boss1returnThis = returnThis.bind(boss1)
boss1returnThis() // boss1
var boss2 = { name: 'boss2' }
boss1returnThis.call(boss2) // still boss1
boss1returnThis.apply(boss2) // still boss1
boss1returnThis.bind(boss2) // boss2

相关文章:

  • 2022-01-05
  • 2021-12-24
  • 2021-12-02
  • 2022-12-23
猜你喜欢
  • 2021-12-03
  • 2021-12-31
  • 2021-09-21
  • 2021-07-11
  • 2021-08-27
  • 2022-03-08
  • 2021-09-19
相关资源
相似解决方案