How to set a prototype for a JSON object?
casting, javascript, json, oop
Solution
OK. Here is the answer (which is IE-incompatible):
json.__proto__ = MyClass.prototype;
Thankfully, I don't need no %$@$%# IE in my application.
(When I do, there is another possibility: create a wrapper function in MyClass prototype which copies all properties from JSON to the new object; shallow-copying should be enough).
Problem
I am receiving some JSON object from the server, and I want to 'typecast' or 'bless' it to an object with already defined methods. Is there any way to set a prototype for a plain JSON object? ``` function MyClass(someValue) { this.myProperty = someValue; } MyClass.prototype.someMethod = function() { return "Here's " + this.myProperty + "!"}; var json = {myProperty : 'someValue'}; // ??? json.prototype = MyClass doesn't work, of course. var result = json.someMethod(); ``` How can I do that?