【发布时间】:2016-01-27 02:07:45
【问题描述】:
我很好奇,因为我收到“未定义不是函数”错误。考虑以下类:
var FlareError = require('../flare_error.js');
class Currency {
constructor() {
this._currencyStore = [];
}
static store(currency) {
for (var key in currency) {
if (currency.hasOwnProperty(key) && currency[key] !== "") {
if (Object.keys(JSON.parse(currency[key])).length > 0) {
var currencyObject = JSON.parse(currency[key]);
this.currencyValidator(currencyObject);
currencyObject["current_amount"] = 0;
this._currencyStore.push(currencyObject);
}
}
}
}
currencyValidator(currencyJson) {
if (!currencyJson.hasOwnProperty('name')) {
FlareError.error('Currency must have a name attribute in the json.');
}
if (!currencyJson.hasOwnProperty('description')) {
FlareError.error('Currency must have a description attribute in the json.');
}
if (!currencyJson.hasOwnProperty('icon')) {
FlareError.error('Currency must have a icon attribute in the json.');
}
}
static getCurrencyStore() {
return this._currencyStore;
}
};
module.exports = Currency;
抛开重构,问题出在:this.currencyValidator(currencyObject); 我收到错误“未定义不是函数”
我认为这是因为我有一个内部调用非静态方法的静态方法?该非静态方法必须是静态的吗?如果是这样,this.methodName 的概念是否仍然有效?
【问题讨论】:
-
我想知道您为什么决定将
store设为静态,因为您似乎不知道static的含义。 -
当然,静态方法可以调用非静态方法当它有一个实例可以调用它时。你的实例在哪里?
标签: javascript class ecmascript-6 static-methods