title: Accessing an Objects instance constructor for cloning
body: <p>TLDR: Are there any pitfalls in using this.constructor(props) in a class method to duplicate that class?</p>
  <p>I have a use case where defining class models for entities proves quite useful for management and processing throughout a complex system.</p>
  <p>These entity model classes all require a set of common methods.</p>
  <p>I do not wish to duplicate code across multiple entity model classes. I am exploring the use of a BaseEntityModel class which can be extended by all other model classes requiring the common functionality.</p>
  <p>One of these methods is a <code>clone()</code> method, which provides a deep clone of the object as the ExtendedEntityModel type.</p>
  <p><code>clone()</code> will be declared on the BaseEntityModel class. I want to preserve the class name and properties when calling clone from a ExtendedEntityModel object which has extended BaseEntityModel class.</p>
  <p>I need a way of creating a new instance of a ExtendedEntityModel through the BaseEntityModel without having to explicitly know what it is when calling <code>clone()</code>. In otherwords, I wanted to avoid using <code>new ParentClassEntity()</code> because the BaseEntityModel methods need to work for all extensions of it.</p>
  <p>I did some searching but could not find much on this pattern. After some tinkering, I found that calling <code>this.constructor(props)</code> does exactly what I want. See below class, and <code>clone()</code> function</p>
  <p>BaseEntityModel.js</p>
  <pre><code>class BaseEntityModel {
    constructor({ entityType, dataKeys }) {
      this.entityType = entityType;
      this.dataKeys = dataKeys;
    }
  
   /**
    * Provide deep clone of self, Preserving extended class properties
    */
    clone() {
       return new this.constructor(JSON.parse(JSON.stringify(this))); //&lt;-- here, this.constructor
     };
  }
  </code></pre>
  <p>ExtendedModel.js</p>
  <pre><code>class ExtendedModel extends BaseEntityModel {
  
      constructor({param1, param2} : ExtendedModel) {
          super({ entityType, dataKeys });
          this.param1 = param1;
          this.param2 = param2;
      }
  
  }
  </code></pre>
  <p>and then usage</p>
  <pre><code>var extendedModel1 = new ExtendedModel({param1, param2};
  var clonedExtendedModel = extendedModel1.clone(); //returns a cloned version and of type ExtendedModel
  </code></pre>
  <p>I cannot find much on calling <code>this.constructor</code>. Can anyone provide some insight into why this might be a bad choice, or confirm that this is not a javascript bomb waiting to go off?</p>
  <p>See <a href="https://codesandbox.io/s/fancy-fog-68jxq?file=/src/App.js" rel="nofollow noreferrer">CodeSandbox</a> of working implementation</p>