首页 > 其他分享 >js函数( 普通函数、箭头函数 ) 内部this的指向

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

时间:2022-09-04 11:57:20浏览次数:95  
标签: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,函数
From: https://www.cnblogs.com/hello-world-01/p/16653979.html

相关文章

  • 为什么要在学习花哨的 JS 框架之前学习 Vanilla JavaScript?
    为什么要在学习花哨的JS框架之前学习VanillaJavaScript?这是DavidKopal的博客。在此博客中,您可以了解VanillaJS在您的职业生涯中的好处。他在博客中分享的文字......
  • Linux 下安装 node.js
     这里介绍两种安装方式:编译安装和使用编译后的安装包安装。安装目录:/usr/local 一、使用编译安装包安装1、进入安装目录:cd/usr/local2、下载安装包:wgethttp......
  • 第六章 1 函数-基础 练习题
    第六章1函数-基础练习题[基础知识]1可以使用内置函数_______________查看包含当前作用域内所有全局变量和值的字典globals().print2可以使用内置函数_______________......
  • numpy数组扩展函数repeat和tile用法
    numpy数组扩展函数repeat和tile用法【Python学习】Numpy函数repeat和tile用法 ......
  • 前端JS-Day19
    动态创建元素:document.write:直接写入页面流,会导致页面重绘。innerHTML和createElement相似,但执行效率不同。创建内容时若采取数组形式,innerHTML耗时优于createElement。......
  • Python内置函数-isinstance() 函数详解
    描述isinstance()函数来判断一个对象是否是一个已知的类型,类似type()。isinstance()与type()区别:type()不会认为子类是一种父类类型,不考虑继承关系。is......
  • JS Layui table 选中行颜色变化
    table设置lay-filter属性<tableid="dataList"lay-filter="dataList"></table>监听单击行事件layui.table.on("row(dataList)",function(obj){$(".layui-tabl......
  • MySQL教程 - 存储过程与自定义函数(Produce & Function)
    更新记录转载请注明出处。2022年9月4日发布。2022年9月4日从笔记迁移到博客。存储过程与函数说明存储过程和函数是一条或多条SQL语句的集合存储过程的返回值通......
  • C语言字符串处理函数 puts()和fputs()的区别及使用
    ​字符串函数(Stringprocessingfunction)也叫字符串处理函数,指的是编程语言中用来进行字符串处理的函数。本文主要介绍C语言中符串处理函数puts()和fputs()的区别使用方......
  • 14个基本初等函数的导数
    14个基本初等函数的导数。 ......