title: Is it possible to instantiate a javascript class from a dynamic variable?
body: <p>I'm wondering if I can do something like the following in Javascript:</p>
  
  <ol>
  <li>Have a set of ES6 classes that I have defined (we'll call these components)</li>
  <li>Have a function where I pass the name of the class and then within the function I instantiate a new class based on the argument that was passed</li>
  <li>Do stuff with the new instantiated class</li>
  </ol>
  
  <p>So if I have some code like the following in a components.js file:</p>
  
  <pre class="lang-js prettyprint-override"><code>export class ComponentOne {}
  
  export class ComponentTwo {}
  
  export class ComponentThree {}
  </code></pre>
  
  <p>Then in another file I have the following code:</p>
  
  <pre class="lang-js prettyprint-override"><code>import {ComponentOne, ComponentTwo, ComponentThree} from './components';
  
  function addComponentToEntity(componentId, entityId) {
    //componentId is the name of the Class that needs to be instantiated
    let comp = new componentId;
  
    //Do stuff with the newly instantiated class and add it to the entity
  }
  </code></pre>
  
  <p>Is it possible to instantiate a class from a variable like I've outlined above? Or what is the correct way of doing this?</p>
  
  <p>Thank you for your help!</p>