我有一个ES6类,从一个函数调用实例化变量,但问题是似乎该函数在构造函数实例化之前运行并抛出错误:
constructor() {
this.userSelections = {
types : this.getTypes(),
providers: this.getProvider()
}
}
getProvider() {
// here its throw error that this.userSelections is undefined
var activeType = this.userSelections.types.some(( type ) => {
return type.active;
});
}
什么是问题,我该如何处理这种情况?
this
引用了userSelections
对象。他正在调用userSelections
对象上的getProviders
方法。 - Danthis.getProvider
时,this.userSelection
已经存在了。我们在前面的语句中创建了它:this.userSelections = {types: this.getTypes()};
- Felix Kling