简体   繁体   中英

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?

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 })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM