简体   繁体   English

使用ES6创建此实例

[英]Create new instance of this using ES6

Is it possible to create a new instance of self/this using ES6 inside a static method? 是否可以在静态方法中使用ES6创建self / this的新实例? For example; 例如;

class myClass {

  static model() {

    return new this;
  }
}

Is there a recognised pattern for this type of scenario? 这种情况是否存在公认的模式?

Many thanks. 非常感谢。

Yes, this is exactly how you'd do it. 是的,这就是您的做法。

If you don't want subclasses to use the subclass constructors, refer to your class by name explicitly, similar to accessing other static methods . 如果您不希望子类使用子类构造函数,则可以通过名称显式地引用您的类,类似于访问其他静态方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ES6 使用 static 方法创建新实例为子 class 属性提供未定义 - ES6 using static method to create new instance gives undefined for child class attributes ES6 - 使用 javascript es6 从基于键名的数组创建新数组 - ES6 - Create new array from an array based on key name using javascript es6 通过使用es6获取数组实例来延迟 - Lag by taking an instance of an array using es6 使用 es6 将新属性插入 object - Insert new property to object using es6 使用 es6 创建带有本机反应的计时器 - create timer with react native using es6 在该类的新实例上重置类参数:JS ES6 - Resetting a class parameter upon new instance of that class: JS ES6 来自现有对象的新ES6类实例 - New ES6 class instance from an existing object ES6 - 从现有数据创建新的 object,只使用一个属性,并删除任何重复项? - ES6 - Create new object from existing data, using only one property, and removing any duplicates? 将ES6类存储在变量中,并通过Object.Create对其进行实例化 - Store ES6 class in variable and instance it through Object.Create 嵌套数组 object 与另一个元素数组比较并使用 Javascript 或 ES6 创建新数组 - nested array object comparison with another array of elements and create new array using Javascript or ES6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM