Skip to content
Advertisement

Reflect API and Serialization via JSON.stringify

The following JavaScript code does something I didn’t expect.

JavaScript

Why is the serialized output from JSON.stringify missing the new bar property?

Advertisement

Answer

because this property is not enumerable.

By default, properties added using Object.defineProperty() are not writable, not enumerable, and not configurable.

see documentation : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#description

JavaScript

so you must do:

JavaScript

This is similar for the length property upon arrays:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement