0

Possible Duplicate:
Parse JSON String into a Particular Object Prototype in JavaScript

So I have a class defined like

function ThisClass (a,b,c) {
    this.a = a;
    this.b = b;
    this.c = c;
}
ThisClass.prototype.a_method = function(param) {
    //stuff
}


var instance = new MyClass(a,b,c);
var stringified = JSON.stringify(instance);
var parsed = JSON.parse(stringified);

When I stringify this and store it in a database it's all cool. However, when I parse it after retrieval, it loses its class type, so when viewing it after parsing in chrome's inspect tool, the object is shown as just a regular object, like instance: Object instead of instance: MyClass.

Because of this, the newly parsed variable parsed does not have any of the prototype methods associated with it at instantiation. Any know how to do this, or if I'm doing it wrong?

Thanks!

1
  • yeah i think you're right, thanks! Commented Sep 17, 2012 at 3:21

1 Answer 1

-1

You can not, JSON is only meant to be used to serialize non circular data object variable.

This the spec for more détails.

Sign up to request clarification or add additional context in comments.

3 Comments

There is no reason to suspect there is a circular reference issue. (There are other things that are true in this context, that not being a good one.)
I've never said they where, I just tried to make a useful statement. The second thing to know after you cant serialize functions is about the circular references. I was just trying to be helpful. Thanks for the down vote. I hope it help you feeling better about yourself. Because it does make you look like a jerk.
What reason is there for me to down-vote this post? (I didn't. So, who "looks like the jerk" now? ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.