为啥箭头函数没有this指向???

为啥箭头函数没有this指向???

 

箭头函数的this指向,指向声明它的地方 https://blog.csdn.net/qq_32614411/article/details/80897256

因为箭头函数没有自己的作用域。所以箭头函数的this会和调用者共享一个作用域。这是es6出的。在适合的条件下可以解决一些问题

 

箭头函数内部的 this 会在编译的时候被 变量替代:

// 编译前书写的箭头函数
let test = {
    foo: "apple",
    getFoo() {
        return () => {
            return this.foo;
        }
    }
}

// 编译后
let test = {
    foo: "apple",
    getFoo() {
        let _this = this;
        return function() {
            return _this.foo;
        }
    }
}