Why don't we see 'o's 'b' property when we log 'o'?

Viewed 76

Looking at the example given on this MDN page

const o = {a: 0};

Object.defineProperty(o, 'b', { get: function() { return this.a + 1; } });

console.log(o.b) // Runs the getter, which yields a + 1 (which is 1)

console.log(o)

Why, when I log o, why do I:
Not see it in node.js?
See it but it's greyed out in Chrome?

1 Answers

If you add enumerable: true to the defineProperty() options object, you'll see it. By default, properties added with .defineProperty() are not enumerable.

Some console environments might choose to show all properties, others don't.

Related