【发布时间】:2016-12-23 00:49:42
【问题描述】:
我正在尝试在我的箭头函数中访问它:
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;
}
});
)};
但是在回调函数里面的箭头函数里面是未定义的!!
我正在使用 babel 转译代码:
myOutsideFunction: function myOutsideFunction() {
var _this = this;
myObject.getMyList(function (myList) {
_this.list = myList;
});
},
【问题讨论】:
-
绑定
this,或将其存储在箭头函数范围之外的临时变量中。 -
this的值取决于myOutsideFunction的调用方式。怎么称呼? -
@Jite 使用箭头函数的整个想法是不必绑定 this!
-
@Felix Kling myOutsideFunction 是从类中的另一个函数调用的。但这应该没关系,因为 myOutsideFunction 有这个。
-
@Jite Np :)) 虽然它对我不起作用:D
标签: javascript this undefined arrow-functions