NodeJS:从父级访问子级的静态方法

时间:2018-07-22 03:43:46

标签: javascript node.js static

我正在使用ES6:

  for i in range(len(s)):
    if s[i:i+3] == "bob":
        count += 1
  print("Number of times bob occurs is: " + str(count))

我想生N个孩子。是否可以动态获取父类内的子类并调用其静态方法?换句话说,如何在父类中泛化ChildN?

1 个答案:

答案 0 :(得分:0)

通过访问父类的静态方法内的this,您将引用子类,因此只需执行this.sayHi()

class Parent {
  static sayHello(){
    this.sayHi()
  }
}

class ChildOne extends Parent {
  static sayHi(){
    console.log('hi from ChildOne')
  }
}

class ChildTwo extends Parent {
  static sayHi(){
    console.log('hi from ChildTwo')
  }
}

ChildOne.sayHello()
ChildTwo.sayHello()