Closed kirkouimet closed 8 years ago
Without this proposal, you wouldn't be able to do that, since it'd just be properties set in the constructor. Typically if you want instance properties visible via reflection on the prototype,', you assign prototypical default values on the prototype
that the constructor then shadows.
@ljharb is correct that it's not possible in the proposal as-is. There was discussion early on about adding a reflection API that would allow you to extract the fields/some descriptor(s) of the fields on a class object.
That API likely won't make it in to this proposal (need to make progress, so need to limit feature creep), but there should be nothing that prevents such a thing from being proposed and introduced later. I think such an API would be worth exploring.
Does this proposal allow me to enumerate through the properties of an instance of a class without instantiating it?
Let's say I have a class like this:
I want to be able to know, without doing
new Person()
that thePerson
class has the propertiesfirstName
andlastName
and what their default values are. The current way that Babel is transpiling this class is like this:Because the assignments are done in the constructor, I have no insight into what properties will exist on the class until a class instance is instantiated. It would be super useful for the code to be transpiled like this:
Knowing what properties a class instance has prior to instantiating it is quite useful when writing reflection code.