In the following example, I use Kyle Simpson's "OLOO (Object Linking to Other Objects) Pattern" to create a simple example of serializing an object.
I need to save the _data private variable (I use closure) and set its properties only using getter and seters, which are created at the instance level of the object (in init ).
I have currently defined a toJson function inside init so that it can access _data and it works, but I would like to know:
- If you could move the
toJson function outside of init (put it on the same level fromJson ) so that it is accessible through the protoype chain, but allows access to the _data variable (I suspect that this is impossible because it is _data in a closure). - Otherwise, could you suggest a better alternative (even using ES6), considering that
_data cannot be enumerated and cannot be modified using a getter and setter?
javascript ecmascript-6 ecmascript-5
GibboK Nov 03 '16 at 7:14 2016-11-03 07:14
source share