[JSON] Changing the class of an object after its construction
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

[JSON] Changing the class of an object after its construction

From: Raphael Jolivet <raphael.jolivetNO_SPAM_THANKS@laposte.net>
Date: Sun Nov 20 2005 - 16:14:23 CET

Hello there,

I have a bunch of objects that I store in a text file (to store
preferences),
after having serialized them with JSON.

Let's say I have my object :

obj1 = {
        a : 1
        b : 2
}

Then, serialized in a string :
"{a : 1, b:2}";

To get the object back (when loading it from prefrences), i do almost :
obj1 = eval("[a:1,b:2");

Ok, my object is now back, but actually, the source object was
a 'myClass' object, with lot of cool methods in it.

What if I want my new object to have all these cool methods ?
I've tried
obj1.prototype = new myClass();
But it doesn't work.

If I do :
obj1.coolMethod = (new myClass()).coolMethod,
I can get 'coolMethod' working fine.
But I don't want to list all my methods.
This is not nice for maintenance and not nice at all.

Any Idea ?

Thanks in advance,

Raphael
Received on Mon Nov 21 03:36:38 2005