2

My question is about ECMAScript 2015 javascript classes.

When I create a new class :

class MyRandomClass {
  constructor() {...}
}

I'd like to access the class constructor I just defined from its name, like I would do with a function :

var myClass = window['MyRandomClass'];

But it does not work, since the class constructor is not stored in the global window object.

Does anyone know how I could get the constructor from its name ?

I don't want to use a regular function as a class.

Using window.MyRandomClass = MyRandomClass does not suit my needs either.

Thanks a lot.

Gin Quin
  • 966
  • 7
  • 15
  • 1
    Why do you need it accessible via the window? Also, if a class can't be used, why not just make it a constructor function, since that is all classes are... – evolutionxbox Jun 26 '17 at 14:19
  • You can look into reflection, it's a cool feature available in multiple language. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect – Nicolas Jun 26 '17 at 14:21
  • Classes are syntactic sugar for mere variables with function values which are just objects, so you're really just asking about *variable variable access*. – deceze Jun 26 '17 at 14:29
  • @deceze Your flagging is completely wrong. This is not a duplicate and warrants it's own answer. – EyuelDK Jun 26 '17 at 14:32
  • 1
    @Nicolas Exactly how would `Reflect` help in this case? –  Jun 26 '17 at 14:32
  • You can use it to call the constructor of a class by its name. `Reflect.construct('MyRandomClass', [args...]);` – Nicolas Jun 26 '17 at 14:35
  • @EyuelDK Go ahead then. – deceze Jun 26 '17 at 14:35
  • `MyRandomClass.name === 'MyRandomClass'` – Jared Smith Jun 26 '17 at 14:41
  • Ah, I hoped Reflect would work, but it actually doesn't. The syntax expected is `Reflect.construct(MyRandomClass, [])` and not `Reflect.construct("MyRandomClass", [])` (it throws an error), so the problem remains unsolved : I can't access MyRandomClass from "MyRandomClass". – Gin Quin Jun 26 '17 at 16:25
  • I finally found a solution using `eval`. see here https://stackoverflow.com/questions/37711603/javascript-es6-class-definition-not-accessible-in-window-global/ – Gin Quin Jul 19 '17 at 23:32

1 Answers1

1

Note that MyRandomClass is the equivalent to it's constructor, i.e. they are the same thing. In Javascript, a class IS a function and the class syntax is only syntactic sugar.

class MyRandomClass {
  constructor() {
  }
}

this.MyRandomClass = MyRandomClass;

console.log(this['MyRandomClass']);
console.log(this['MyRandomClass'].prototype.constructor);
console.log(this['MyRandomClass'].prototype.constructor === this['MyRandomClass']);

Thus accessing the class is the same as accessing it's constructor. Thus, you can access it using this['MyRandomClass'] in the global scope.

EyuelDK
  • 3,029
  • 2
  • 19
  • 27
  • How does that solve the problem of accessing it by string variable? – deceze Jun 26 '17 at 14:37
  • Worth noting you can also get it from an instance: `foo.constructor.name`. – Jared Smith Jun 26 '17 at 14:37
  • @deceze if he can access the variable that points to the class he can access the constructor. He already wrote var myClass = window['MyRandomClass'], thus being a solution. What I understood from his question is that he wasn't able to access the constructor because it wasn't a global object, but my answer shows that as long as he can access the class, he has access to the constructor. – EyuelDK Jun 26 '17 at 14:41
  • Quite frankly I think you misunderstood the question and got hung up on the "constructor" keyword. It would appear to me that I *wasn't* ["completely wrong"](https://stackoverflow.com/questions/44761825/how-to-get-a-javascript-class-from-its-name#comment76505073_44761825). – deceze Jun 26 '17 at 14:43
  • The OP's point is that `window['MyRandomClass']` ***does not work***. – deceze Jun 26 '17 at 14:45
  • @deceze I read a bunch of times and I still fail to see your point. What is your point? That this is a duplicate of some question regarding how to access properties of an object? You say I'm hung up on the constructor keyword, and to that I would say: Yes I am... because that is what the OP clearly stated. Unless you have a different understanding that you would like to explain, I double down and say your flagging is completely wrong! – EyuelDK Jun 26 '17 at 14:53
  • Since "constructor" and "class" are identical, let's rewrite OPs question a bit: *"I'd like to access the class by its name, e.g.: `var myClass = window['MyRandomClass'];` But it does not work, since the class is not stored in the global window object. Does anyone know how I could get the class by its name? __`window.MyRandomClass = MyRandomClass` does not suit me.__"* – deceze Jun 26 '17 at 14:59
  • @deceze Bro, if you have an answer, just share it - post your answer. This is my interpretation of OP's question - I do the best I can given the ambiguity. If you believe this is a duplicate then just repost it and let's see what happens. This question already has two up votes, which non are mine, and you seem to be the only one who disagrees. So just post your answer. – EyuelDK Jun 26 '17 at 15:05
  • This solution would work, but writing `this.MyRandomClass = MyRandomClass;` is the same as `window.MyRandomClass = MyRandomClass;` most of the times, and I specified this solution does not suit my needs. From what I read in this post and others, I guess there is no perfect solution. Thanks anyway. – Gin Quin Jun 26 '17 at 16:25
  • Reflection seems to be your only option then. – EyuelDK Jun 26 '17 at 16:50