The correct way to assign the __proto__ property

I have some objects deserialized from JSON to which I would like to assign a new prototype to provide various getter and setter functions. The obvious way to do this (as mentioned in this question ) is to install

myJsonObj.__proto__ = { function1: /* ... */, function2: /* ... */ }; 

However, since MDC is useful to indicate , the __proto__ property is non-standard and deprecated. Is there any standard-compatible way (for some definition of "standards") to achieve the same effect without creating many new wrapper objects?

+6
json javascript prototype
source share
1 answer

There is no standard way to change the prototype of an object after its creation. There is a standard way to create objects with any prototype you want when parsing from JSON.

From http://www.json.org/js.html :

An additional parameter to reviver is a function that will be called for each key and the value at each level of the final result. Each value will be replaced by the result of the restore function. This can be used to reform generic objects into instances of pseudo-classes, or convert string dates to Date objects.

+4
source share

All Articles