6

The title says it all. The only thing I can find is that global variables are stored under window but that doesn't seem to be true for classes defined using the class keyword.

class Foo {
  constructor() {
   console.log('A Foo is created');
  }
}

const foo = new Foo();
const bar = new window['Foo'](); // TypeError: window.Foo is not a constructor

I know one can write something like let Foo = class Foo {} but I would like to keep the redundancy low. Also, I'm interested if a true, non-work-around solution exists just out of curiosity.

Chris K
  • 1,376
  • 1
  • 14
  • 18
  • 2
    I don't think it is a duplicate of that, that doesn't deal with classes. – Chris Charles Nov 15 '16 at 13:34
  • 2
    The solutions proposed in the possible duplicate don't work for me. Neither `window['Foo']` nor `this['Foo]`. November 2016, Firefox 49.0.2. – Chris K Nov 15 '16 at 13:35
  • 2
    See also [Do `let` statements create properties on the global object?](http://stackoverflow.com/q/28776079/1048572) - you'd need to use `var` not `const` or `let`. For your actual use case, see [Create object from string in JavasScript ECMAScript 6](http://stackoverflow.com/q/31776949/1048572) – Bergi Nov 15 '16 at 13:48

0 Answers0