Getters defined within a class are not enumerable because they are attached to the class prototype.
Making them enumerable is not sufficient to have them picked up by Object.keys() or Object.hasOwnProperty()
so the values will not usually be exposed without a specific request (object.foo
).
In the code example, the getter and setter is defined in class scope as expected. A backing variable is defined within the setter, possibly overwriting an existing property if already assigned. This is set to non-enumerable so that it is not exposed to Object.keys().
The getter simply returns the assigned value.
In the constructor, the prototype for the property is copied from the class prototype to the instance, whilst ensuring that instance property is enumerable.
The second code example shows the same thing but with some optimisations to better deal with multiple properties.