Ecmascript 6 在ES6中继承静态方法

Ecmascript 6 在ES6中继承静态方法,ecmascript-6,Ecmascript 6,使用ES6语法是否可以扩展类并继承其静态方法?如果是这样,我们可以在子类的静态方法中调用super吗 例如: class Parent { static myMethod(msg) { console.log(msg) } } class Child extends Parent { static myMethod() { super("hello") } } Child.myMethod(); // logs "hello" 这给了我一个对transp

使用ES6语法是否可以扩展类并继承其静态方法?如果是这样,我们可以在子类的静态方法中调用super吗

例如:

class Parent {
  static myMethod(msg) {
    console.log(msg)
  }
}

class Child extends Parent {
  static myMethod() {
    super("hello")
  }
}

Child.myMethod();  // logs "hello" 
这给了我一个对transpiler(Reactify)中未定义错误的无方法调用

\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

根据当前
对象的原型的和
super
基引用。在静态方法中,它将引用继承的类。因此,要调用父静态方法,必须调用
super.myMethod('some message')
。以下是一个例子:

class Parent {
  static myMethod(msg) {
    console.log('static', msg);
  }

  myMethod(msg) {
    console.log('instance', msg);
  }
}

class Child extends Parent {
  static myMethod(msg) {
    super.myMethod(msg);
  }

  myMethod(msg) {
    super.myMethod(msg);
  }
}

Child.myMethod(1); // static 1
var child = new Child(); 

child.myMethod(2); // instance 2

请注意,调用
super
外部构造函数是非法的。您可能想在这里使用
super.myMethod()
,但我不知道
super
是否应该在静态方法中工作。有趣的问题,但从OOP的角度来看,您不应该调用
Parent.myMethod()
,因为
static
并不意味着任何实例吗?