[JS] List all variables of a class

Hello !

Sorry in advance for my poor english.

There is my problem :
I have a class “Module” with some basics variables in it, like “name”, “description” etc…

I have also other classes that inherits from Module (“Engine”, “Reactor”), those implements some more variables (“Energy consumption”, etc…).

Then I have a script that must build a GUI that list the variables of an object, independantly if it’s an “Engine”, “Reactor”, or other.

So OK, I can make a script with a succession of “if” conditions to verify if it is an Engine or a Reactor etc… But I have a long list of type of modules, so it will be very long. And if I add a new type of module, like “weapon”, I also must add another “if” condition to my script.
I don’t want that.

So is there a solution to list all variables attached to an object ?

Thank you for your help !

Istrella

It’s called reflection, but I have no idea how to do it in JS. In C#, you use System.Reflection and you can list PropertyInfo, FieldInfo and MethodInfo of any type.

Thank you !

Anyone can give me a short example in JS for how to use this ?

Thanks in advance :slight_smile: