0

See this script: https://jsfiddle.net/t5w060bv/

var A = function() {
  this.test1 = '1';
};
Object.defineProperty(A.prototype, 'test2', { value: '2', enumerable: true });
document.body.innerText = JSON.stringify(new A());

Even though the property is defined as enumerable, it is not serialized. What's the correct way to handle this situation?

Bart van den Burg
  • 2,166
  • 4
  • 25
  • 42
  • See [this question](http://stackoverflow.com/questions/12369543/why-is-json-stringify-not-serializing-prototype-values). Basically, that's the way `JSON.stringify()` has been defined by ES5. – Phylogenesis Mar 05 '15 at 09:42

1 Answers1

1

the reason is JSON.stringify only serializes it's own property not the prototype one.

To test it

a.hasOwnProperty('test2'); // will return false

a.hasOwnProperty('test1'); // will return true

so whatever return true , gets serialized.

Community
  • 1
  • 1
invinciblejai
  • 1,103
  • 1
  • 8
  • 15