this 在箭头函数中未定义

this is undefined inside arrow function

我试图在我的箭头函数中访问它:

import myObject from '../myObjectPath';
export const myClass = Fluxxor.createStore({
    initialize() {
        this.list = [];
        this.id = null;
    },
    myOutsideFunction(variable1) {
        // here this in NOT undefined
        myObject.getMyList(this.id, (myList) => {
            // here this in undefined
            this.list = myList;
        } 
    });
)};

但是在 ma 回调函数中的箭头函数中 this 是未定义的!!

我正在使用 babel 转译代码:

myOutsideFunction: function myOutsideFunction() {
    var _this = this;
    myObject.getMyList(function (myList) {
        _this.list = myList;
    });
},

如果 this 在箭头函数中是 undefined,那么它在箭头之外也是未定义的。箭头函数简单地捕获周围作用域的this

在这种情况下,您将 myOutsideFunction 声明为对象文字上的方法,并且从不绑定它或做任何其他会用对象 this.[=17 调用它的事情=]

调试时,请记住转译器可以重命名变量(并且必须重命名 this 才能正确捕获)。在没有包含重命名的源映射的控制台中使用原始名称将向您显示 undefined,即使原始值不是。确保在手表或控制台命令中使用转换后的名称。