• 几句话,让你搞懂this指向问题


    先说结论

    • 函数被谁调用,this就是谁,如果没有被对象调用,this就是window
    • 通过new声明的构造函数,函数内容的this永远指向函数本身
    • 箭头函数没有this,箭头函数内部的this取决于外部定义时的环境
    • 箭头函数的this指向外层函数的this,如果外层也不存在this,会查找到window

    常见的几种this使用场景

    声明式

    function test1() {
      console.log(this)
    }
    test1() // window
    

    匿名函数/赋值式

    var test2 = function () {
      console.log(this)
    }
    test2() // window
    

    对象中定义

    var a = 1
    var obj = {
      a: 2,
      test3: function () {
        console.log(this)
        console.log(this.a)
      }
    }
    obj.test3() // obj 2
    

    循环中

    // 循环中
    [1, 2, 3].forEach(function () {
      console.log(this) // window
    })
    

    箭头函数

    var obj2 = {
      test4: function () {
        console.log(this)
      },
      test5: () => {
        console.log(this)
      },
      test6: function () {
        var test7 = () => console.log(this)
        test7()
      },
      test8: () => {
        var test9 = () => console.log(this)
        test9()
      }
    }
    
    obj2.test4()  // {test4: ƒ, test5: ƒ, test6: ƒ, test8: ƒ}
    obj2.test5()  // window
    obj2.test6()  // {test4: ƒ, test5: ƒ, test6: ƒ, test8: ƒ}
    obj2.test8()  // window
    

    new

    var a = 1
    function foo() {
      console.log(this)
    }
    var obj = new foo()
    obj.a = 2
    console.log(obj)  // foo {a: 2}
    console.log(obj.a)  // 2
    

    几种改变this指向的方式

    call()

    var obj1 = {
      name: 'xcc',
      age: 20,
      sex: '男'
    }
    
    var obj2 = {
      name: 'xcc1',
      age: 28,
      test1: function () {
        console.log(this)
      }
    }
    
    obj2.test1()  // {name: "xcc1", age: 28, test1: ƒ}
    
    obj2.test1.call(obj1, '123') // {name: "xcc", age: 20, sex: "男"}
    

    apply()

    var obj1 = {
      name: 'xcc',
      age: 20,
      sex: '男'
    }
    
    function test2(name, age, sex) {
      console.log(name, age, sex, this)
    }
    
    test2.apply(obj1, ['xcc2', 18, '女']) // xcc2 18 女 {name: "xcc", age: 20, sex: "男"}
    

    bind()

    var obj1 = {
      name: 'xcc',
      age: 20,
      sex: '男'
    }
    
    function test2(name, age, sex) {
      console.log(name, age, sex, this)
    }
    
    var test3 = test2.bind(obj1)
    test3('xcc3', 30, '男')   // xcc3 30 男 {name: "xcc", age: 20, sex: "男"}
    

    参考资料

  • 相关阅读:
    C#中利用iTextSharp开发二维码防伪标签(1)
    delphi 数据库中Connection与Query连接数量问题思考
    cPanel 安装方法
    招商行用卡人工服务方式
    软链接的创建和查看
    zencart低版本由php5.2.17升级PHP5.3环境下错误及解决方案
    EXCEL应用:高级筛选里的条件或和与的条件怎么写 例:不包含,包含等
    array_walk与array_map 的不同 array_filter
    zen cart global $db 这噶哒
    hdu 5655 CA Loves Stick
  • 原文地址:https://www.cnblogs.com/sk-3/p/14663608.html
Copyright © 2020-2023  润新知