ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-09-04 12:04:15  阅读:179  来源: 互联网

标签:function obj log js 箭头 String console hello 函数


- 普通函数

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

    示例:

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

  

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

    示例:

    (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 指向该属性所直接归属的对象 。

    示例:

    (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。

    示例:

    (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 的指向,虽不会报错但却是无效的。

    示例:

    (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) // a
            console.log(b) // b
            console.log(c) // c
        }).bind(null, 1, 2)(3)
    }).bind('hello')()

  

  | 附:

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

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

  

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

        (function() {
            ((a) => {
                console.log(a) // 1
                console.log(arguments) // Arguments ['hello']
            })(1)
        })('hello');

        (() => {
            console.log(arguments) // Uncaught ReferenceError: arguments is not defined
        })();

 

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

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

  

   

标签:function,obj,log,js,箭头,String,console,hello,函数
来源: https://www.cnblogs.com/hello-world-01/p/16653979.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有