0

I'm trying to understand why using Object.defineProperty makes a property not visible when the object is logged in the console.

For instance:

let foo = {};

Object.defineProperty(foo, "a", {
  get() { return "a"; }
});

Object.defineProperty(foo, "b", {
  get() { return "b"; },
  enumerable: true
});

console.log( foo ); // returns {}

I do understand that enumerable will affect the outcome of a for...in loop or the Object.keys() static method. But I don't get why console.log(foo) returns {}, regardless of the use of enumerable, and even if I start by declaring foo as let foo = { a: 'bar' };

Thanks!

TheCat
  • 640
  • 1
  • 9
  • 23
  • What console are you looking at? Every engine has its own implementation, every runtime comes with its own gui. – Bergi Oct 10 '22 at 15:55
  • I *do* see the getters in the console: https://i.stack.imgur.com/4On21.png – Bergi Oct 10 '22 at 15:57
  • "*even if I start by declaring `let foo = { a: 'bar' };`*" - the `Object.defineProperty` call *overwrites* the initial data property, the result is indistinguishable from starting with an empty object. – Bergi Oct 10 '22 at 15:58
  • 2
    Is `Object.defineProperty` even relevant? For instance, take a look at `const woo={get a(){return 1}}`. To me this question actually reads: "Why do getters appear differently to fields in the console?" – spender Oct 10 '22 at 15:59

0 Answers0