Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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?

like image 802
tonitone120 Avatar asked Aug 04 '20 12:08

tonitone120


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.

like image 63
Pointy Avatar answered Sep 22 '22 16:09

Pointy