繁体   English   中英

这是箭头函数内部未定义的

[英]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回调函数中是未定义的!

我正在使用babel来翻译代码:

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

如果thisundefined箭头函数中,它的箭头的不确定之外也是如此。 箭头功能仅捕获周围范围的this范围。

在这种情况下,您将myOutsideFunction声明为对象文字的方法,并且从不对其进行绑定或执行任何其他操作,例如使用this调用。

在调试时记住,记住,transpilers可以重命名变量(并有重命名this它正确地捕获)。 在控制台中使用不带重命名的源映射的原始名称将显示undefined即使原始值不是。 确保在监视或控制台命令中使用已转换的名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM