bind、Call、Apply的区别

三者作用相同、都是解决this指向问题,传参方式和运行方式

除了第一个参数(默认为window)call只接受参数列表,apply只接受参数数组
let test
= {   value:12 }
function demo(name,sex){   console.log(name)   console.log(sex)   console.log(this.value) }
demo.call(test,
'zachary','man') // zachary man 12 demo.apply(test,['zachary','man']) // zachary man 12

除了字面量的 我们还可以 this 指向函数里面的

function a(){
this.value = 12
}
function demo(){ //a.call(this) a.apply(this) }
let aa
= new demo() console.log(aa.value)

 bind 执行的时候改变this指向  bind是函数执行的时候调用并改变this指向

  function a() {
    this.valu = 99
  }

  function b() {
    a.bind(this)()  // 必须以函数形式运行
  }
  let newa = new b()
  console.log(newa.valu)
小结一下可以这么理解 

call们继承父级属性,prototype继承父级方法
posted @ 2018-12-09 23:31  Model-Zachary  阅读(177)  评论(0编辑  收藏  举报