• js函数( 普通函数、箭头函数 ) 内部this的指向


    - 普通函数

      | 具名普通函数、匿名普通函数,在不作为对象的属性值的情况下,其内部的 this 总是指向代码运行环境下的全局对象 ( 例如,浏览器中的 window )。

        示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    (function() {
        console.log(this); // window
        (function() {
            console.log(this); // window
            (function() {
                console.log(this); // window
            })()
        })()
    })()

      

      | 普通函数,均可以通过其 bind、call、apply 方法 来改变其内部 this 的指向。

        示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    (function() {
        const func = (function() { console.log(this) }).bind('hello')
        const obj = {
            func,
            func1: (function() { console.log(this) }).bind('hello'),
            func2: (function F() { console.log(this) }).bind('hello')
        }
        func() // String {'hello'}
        obj.func() // String {'hello'}
        obj.func1() // String {'hello'}
        obj.func2() // String {'hello'}
    })()

      

      | 当普通函数( 具名的、匿名的、外部定义的方法 ),作为对象的属性值被引用的时候,其内部的 this 指向该属性所直接归属的对象 。

        示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    (function() {
        const func = function() { console.log(this) }
        const obj = {
            func,
            func1: function F() { console.log(this) },
            func2() { console.log(this) },
            param: {
                func,
                func1: function F() { console.log(this) },
                func2() { console.log(this) }
            }
        }
        func() // window
        obj.func() // obj
        obj.func1() // obj
        obj.func2() // obj
        obj.param.func() // obj.param
        obj.param.func1() // obj.param
        obj.param.func2() // obj.param
    })()

      

     


    - 箭头函数

      | 箭头函数,不管是作为独立的方法 或是 作为对象的属性值,其内部的 this 均指向 该箭头函数被定义时所在的上下文中对应的 this。

        示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    (function() {
        /** 外层作用域 */
        const arrowfunc = () => console.log(this)
        
        console.log('-- 外层作用域 --');
        console.log(this); // String {'hello'}
        arrowfunc(); // String {'hello'}
        
        (function() {
            /** 内层作用域 */
            const arrowfunc1 = () => console.log(this)
            
            console.log('-- 内层作用域 --');
            console.log(this); // String {'world'}
            arrowfunc() // String {'hello'}
            arrowfunc1() // String {'world'}
     
            /** 函数作为对象属性值 */
            const obj = {
                arrowfunc,
                arrowfunc1,
                param: {
                    arrowfunc,
                    arrowfunc1,
                    arrowfunc2: () => console.log(this)
                }
            }
            
            console.log('-- 函数作为对象属性值 --');
            obj.arrowfunc() // String {'hello'}
            obj.arrowfunc1() // String {'world'}
            obj.param.arrowfunc() // String {'hello'}
            obj.param.arrowfunc1() // String {'world'}
            obj.param.arrowfunc2() // String {'world'}
        }).bind('world')()
    }).bind('hello')()

      

      | 箭头函数 也有 bind、call、apply 方法,与普通函数一样可以通过这三个方法预设箭头函数的入参值。

        试图通过这三个方法改变箭头函数内部 this 的指向,虽不会报错但却是无效的。

        示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    (function() {
        console.log(this); // String {'hello'}
        (() => {
            console.log(this); // String {'hello'}
            (() => {
                console.log(this) // String {'hello'}
            }).bind('bbb')()
        }).bind('aaa')();
        
        ((a, b, c) => {
            console.log(this) // String {'hello'}
            console.log(a) // 1
            console.log(b) // 2
            console.log(c) // 3
        }).bind(null, 1, 2)(3)
    }).bind('hello')()

      

      | 附:

          * 箭头函数不能作为构造函数使用,强制使用 new 运算符作用在箭头函数上,将会报如下错误

    1
    new (() => {}) // Uncaught TypeError: (intermediate value) is not a constructor

      

          * 箭头函数内部没有定义 arguments 变量,箭头函数所在的作用域也不存在 arguments 的情况下,应用该变量会报错。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    (function() {
        ((a) => {
            console.log(a) // 1
            console.log(arguments) // Arguments ['hello']
        })(1)
    })('hello');
     
    (() => {
        console.log(arguments) // Uncaught ReferenceError: arguments is not defined
    })();

     

          * 普通函数都有原型属性 prototype,箭头函数没有这个属性。

    1
    2
    (function() {}).prototype // {constructor: ƒ}
    (() => {}).prototype // undefined

      

     
     
  • 相关阅读:
    前缀树的设计与实现
    Adafruit_GFX matrix ws2812像素屏库使用教程AWTRIX2.0像素时钟
    【零基础学QT】第十章 项目打包,利用Inno Setup制作软件安装包
    【PAT甲级】1050 String Subtraction
    零基础转行次世代3D建模,这3点看了再转
    在报表控件 FastReport .NET 中使用 PageCreate 事件
    JavaSE——选择排序算法、冒泡排序法、二分查找法
    想在golang里用好泛型还挺难的
    实验六:频域图像增强方法
    mysql约束
  • 原文地址:https://www.cnblogs.com/hello-world-01/p/16653979.html