Swordword
Swordword

Reputation: 171

Different output in Node and browser

const person = { name: 'Bob' }
Object.defineProperty(person, 'age', { value: 21 })
console.log(person)

this code result in different output
in chrome print {name: "Bob", age: 21}
however in Node (v15.6.0), print { name: 'Bob' }
what cause that?

Upvotes: 0

Views: 104

Answers (1)

Federkun
Federkun

Reputation: 36924

If you expand the output of the console log in chrome, you may notice that "age" is faded out (as, it has another color) a bit. Because is a non-enumerable propriety.

console.log only returns enumerable properties. But if you look with Object.getOwnPropertyNames(person) in node, you will see age as well.

What you want to do is:

Object.defineProperty(person, 'age', { value: 21, enumerable: true })

Upvotes: 3

Related Questions