浅谈箭头函数

2022-07-28,,

箭头函数
ES6标准新增了一种新的函数:Arrow Function(箭头函数)。
为什么叫Arrow Function?因为它的定义用的就是一个箭头:

    let Person = function() {
        this.name = "张飞",
        this.age = 18,
        this.obj = {
            name:"关羽",
            // fun: function() {
            //     console.log(this.name)
            // },
            fun: () => {
                console.log(this.name)
            }
        }
    }
    let p = new Person()
    console.log(p)
    p.obj.fun()
    ```
    **箭头函数的this指向规则:**
1. 箭头函数没有`prototype`(原型),所以箭头函数本身没有`this`
2. 箭头函数的`this`指向全局,使用`arguments`会报未声明的错误
3. 箭头函数的`this`指向普通函数时,它的`argumens`继承于该普通函数
4. 不能直接修改箭头函数的this指向
5. 箭头函数的`this`指向在定义的时候继承自己外层第一个普通函数的this。
6. 箭头函数外层没有普通函数,严格模式和非严格模式下它的this都会指向`window`(全局对象)
   

本文地址:https://blog.csdn.net/weixin_47295135/article/details/108880644

《浅谈箭头函数.doc》

下载本文的Word格式文档,以方便收藏与打印。