class RandomObject {
constructor() {
this.method1 = function() {}
}
method2() {
// only this is non enumerable
}
method3 = function() {}
}
const object = new RandomObject()
for (const prop in object) {
console.log(prop) // 'method1', 'method2'
}
I found that only method1 and method2 are printed out in the for...in loop, meaning they are enumerable. However method2 doesn't show up in the loop, meaning it is non-enumerable. I wonder what caused the differences here? Why is that only method2 is non-enumerable in this case?
Also, I found that if I define the method directly in the object literal, then it is enumerable again:
const object2 = {
method2() {
// Now this is enumerable
}
}