Javascript 从父构造函数访问静态子方法/属性
从父类开始:Javascript 从父构造函数访问静态子方法/属性,javascript,es6-class,Javascript,Es6 Class,从父类开始: class Food { constructor(type) { const valid_types = this.getValidTypes(); if (!valid_types[type]) { throw 'Invalid food type.'; } this.type = type; } static getValidTypes() { return null; }
class Food {
constructor(type) {
const valid_types = this.getValidTypes();
if (!valid_types[type]) {
throw 'Invalid food type.';
}
this.type = type;
}
static getValidTypes() {
return null;
}
}
和两个儿童班:
const VALID_FRUIT_TYPES = ["Apple", "Banana", "Tomato"];
class Fruit {
constructor(type) {
super(type);
}
static getValidTypes() {
return VALID_FRUIT_TYPES;
}
}
const VALID_VEGETABLE_TYPES = ["Corn", "Lettuce", "Artichoke"];
class Vegetable {
constructor(type) {
super(type);
}
static getValidTypes() {
return VALID_VEGETABLE_TYPES;
}
}
是否有任何方法可以使父类构造函数
调用相应子类的getValidTypes
?我知道可以声明一个常规的类方法,它将返回我正在寻找的数组,但我更希望在类中静态定义该方法,而不是在类的每个实例上定义该方法,因为它返回一个常量