【问题标题】:Create new instance of this using ES6使用 ES6 创建 this 的新实例
【发布时间】:2017-12-18 19:44:24
【问题描述】:

是否可以在静态方法中使用 ES6 创建 self/this 的新实例?例如;

class myClass {

  static model() {

    return new this;
  }
}

这种场景有公认的模式吗?

非常感谢。

【问题讨论】:

标签: javascript ecmascript-6


【解决方案1】:

是的,这正是你要做的。

如果您不希望子类使用子类构造函数,请通过名称显式引用您的类,类似于 accessing other static methods

【讨论】:

  • @downvoters 请解释为什么您认为这是有争议的 (+2/-2)
猜你喜欢
  • 1970-01-01
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 2021-09-17
  • 1970-01-01
  • 2012-12-15
  • 1970-01-01
相关资源
最近更新 更多