title: How to instantiate a Class from a String in JavaScript
body: <p>I'm in a weird situation that i need to instantiate a new Class with a string stored in a variable but even i'm sure the class name is correct i get an error that given class name is not a constructor</p>
  
  <p>Here is a dummy code that doesn't work:</p>
  
  <pre><code>class Foo {
      constructor(){
          console.log('Foo!');
      }
  };
  const foo = 'Foo';
  const bar = new window[foo]();
  console.log(bar);
  </code></pre>
  
  <p>This trow this error:</p>
  
  <pre><code>Uncaught TypeError: window[foo] is not a constructor
  </code></pre>