Why globalThis does not include classes (like it includes functions)? Is there other 'this' that includes them?
What I want is to instantiate a class from a string, after checking it is defined, but it does not work the same I am doing for functions. For functions I check the function is defined in globalThis and if yes I call it. But for classes does not work.
My code:
class Foo {
constructor() {}
foobar(i) {return i+1;}
}
function bar(i) {
return i+1;
}
function myFunction() {
if (globalThis['bar'])
console.log(globalThis['bar'](3)); // This works fine, it calls the function bar
if (globalThis['Foo'])
console.log((new globalThis['Foo']).foobar(3)); // never reaches here!!
}
I have checked how globalThis
looks and it seems it does not include the class, but only the functions!! :
console.log(globalThis);
gives the following output:
{ myFunction: [Function: myFunction], bar: [Function: bar] }
I am using GAS, but I assume this is javascript generic.