Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从父构造函数访问静态子方法/属性_Javascript_Es6 Class - Fatal编程技术网

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
?我知道可以声明一个常规的类方法,它将返回我正在寻找的数组,但我更希望在类中静态定义该方法,而不是在类的每个实例上定义该方法,因为它返回一个常量