JavaScript: Where are the classes stored? (How are they accessible through a string containing the class name?)

The title says it all. The only thing I can find is that global variables are stored under the window, but this does not look like 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
Run codeHide result

I know you can write something like let Foo = class Foo {}, but I would like the redundancy to be low. In addition, I am interested in whether a true, inoperative solution exists only out of curiosity.

+4
source share

Source: https://habr.com/ru/post/1660790/


All Articles