Proper way of assigning to __proto__ property

javascript, json, prototype

Solution

There is no standards compliant way to change the prototype of an object after its creation. There is a standards compliant way to create objects with whatever prototype you desire while parsing from JSON.

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

The optional reviver parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects into instances of pseudoclasses, or to transform date strings into Date objects.

Problem

I have some objects deserialized from JSON to which I'd like to assign a new prototype in order to provide various getter and setter functions. The obvious way to do this (as mentioned in this question) is to set ``` myJsonObj.__proto__ = { function1: /* ... */, function2: /* ... */ }; ``` However, as MDC helpfully points out, the `__proto__` property is non-standard and deprecated. Is there any standards-compliant way (for some definition of "standards") to achieve the same effect, without having to create lots of new wrapper objects?

Original source

Related problems