0

A simple and obvious issue I've just stumbled on is serializing to JSON using JSON.stringify.

If you're not aware already, it doesn't work, JSON.stringify won't use any of the getters:

class MyClass {
  get m() { return 1; }
}

JSON.stringify(new MyClass()); // writes empty object {} 

Am I missing something regarding JSON serialization? I am already aware of implementing toJSON().

Classes still seem half baked after many years, I am starting to question why use them after all.

Don Box
  • 3,166
  • 3
  • 26
  • 55
  • maybe this already answers your question? https://stackoverflow.com/a/8294127/3125277 – Argee May 08 '20 at 10:29
  • https://stackoverflow.com/a/33386327/1048572 – Bergi May 08 '20 at 10:42
  • Your `m` property is neither enumerable nor an own property of the instance (it's inherited from the prototype). Both of which would be required to make it recognised by `JSON.stringify`. Use `constructor() { this.m = 1; }` instead – Bergi May 08 '20 at 10:45

0 Answers0