zoukankan      html  css  js  c++  java
  • 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继承父级方法
     
  • 相关阅读:
    左偏树
    论在Windows下远程连接Ubuntu
    ZOJ 3711 Give Me Your Hand
    SGU 495. Kids and Prizes
    POJ 2151 Check the difficulty of problems
    CodeForces 148D. Bag of mice
    HDU 3631 Shortest Path
    HDU 1869 六度分离
    HDU 2544 最短路
    HDU 3584 Cube
  • 原文地址:https://www.cnblogs.com/Model-Zachary/p/10094097.html
Copyright © 2011-2022 走看看