Click here to Skip to main content
15,792,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a class in client side using javascript like

function ClassA(Id, value) {

    this.ID = Id;
    this.Value = value; 
    this.ListOfOtherObject = new Array();

    this.methodA = function() {
		// login here

    this.methodB = function() {
		//logic here

Now when I filled my object with value like

Var obj=new ClassA();
obj.Value=’my value’;
 in the end I serialize my class obj using JSON. 
Var jsonStringOfMyObject= SerializeClassAObject(obj);

The function which I am using for serialize purpose is;
function SerializeClassAObject(aStudy) {
                    var t = typeof (aStudy);
                    if (t != "object" || aStudy === null) {
                        // simple data type   
                        if (t == "string") aStudy = '"' + aStudy + '"';
                        return String(aStudy);
                    else {
                        // recurse array or object   
                        var n, v, json = [], arr = (aStudy && aStudy.constructor == Array);
                        for (n in aStudy) {
                            v = aStudy[n]; t = typeof (v);
                            if (t == "string") v = '"' + v + '"';
                            else if (t == "object" && v !== null) v = JSON.stringify(v);
                            json.push((arr ? "" : '"' + n + '":') + String(v));
                        return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");

                        var end = "";
                        //                }

This return the JSON string which I saved on text(.txt) file. Then later when I read the txt file from my drive and de-serialize the JSON string using eval() method like;

eval("var p=" + jsonStringOfMyObject + ";");
  obj = p;

This reconstruct my ClassA obj, with all properties like ID and Value. Now if I try to call methods which are part of my ClassA then it through error “Object doesn't support this property or method”. When I did little R & D on this then I found that eval return the object with properties only and ignore all methods.
Now guys please tell how we can retain the methods of our objects?
Please note that I present simpler example, I my case my object containing other class objects which have their own methods too.
Thank you
Updated 23-Dec-10 21:38pm
maq_rohit 24-Dec-10 2:22am    
Have you debug javascript using chrome javascript debugger...
TweakBird 24-Dec-10 2:38am    
Edited for Spells & <pre> tags

1 solution

Hello Ahmad,

the solution to your problem is $.extend() or jQuery.extend().
I'll give you an example below, but please also go to the jQuery
website here:

and for the JSON plugin to jQuery please read this:
jQuery-JSON Plugin[^]

//look to use jQuery-JSON Plugin here instead of eval
eval("var p= " + jsonStringOfMyObject + ";");
dataFromServer = p;

fullyUsableClassAWithMethodsAndAll = new ClassA();

// the magic starts here
$.extend(fullyUsableClassAObjectWithMethodsAndAll, dataFromServer);
// presto!

// fullyUsabelClassAObjectWithMethodsAndAll is now a ClassA object
// filled with the data returned from the server in form of JSON

If you return more than one type of object form the server you'll have to make sure to create the appropriate instance before the $.extend() part.

I hope you could follow me :) .
If you have any doubts, check back with me.


Share this answer
Dalek Dave 24-Dec-10 16:16pm    
Very Good Answer.
Manfred Rudolf Bihy 24-Dec-10 16:40pm    
Thanks Dave!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900