I have an ES6 class (transcompiled with babeljs) with a getter property. I understand that these properties are not enumerable by default. However, I do not understand why I am not able to make the property enumerable using Object.defineProperty
// Declare class
class Person {
constructor(myName) {
this.name = myName;
}
get greeting() {
return `Hello, I'm ${this.name}`;
}
}
// Make enumerable (doesn't work)
Object.defineProperty(Person, 'greeting', {enumerable: true});
// Create an instance and get enumerable properties
var person = new Person('Billy');
var enumerableProperties = Object.keys(person);
// => ['name']
See Question&Answers more detail:os