4

I've been initializing my reusable classes like this (constructor is usually a copy-constructor):

function Foo() {}
Foo.prototype.a = "1";
Foo.prototype.b = "2";
Foo.prototype.c = [];
var obj = new Foo();
obj.c.push("3");

but the JSON.stringify does not produce the expected result:

JSON.stringify(obj);

{}

The variables work as expected for everything else.
If toJSON is overridden, it works fine:

Foo.prototype.toJSON = function () {
    return {
        a: this.a,
        b: this.b,
        c: this.c
    };
};
JSON.stringify(obj);

{"a":"1","b":"2","c":["3"]}

It also works fine if the variables are defined inside the constructor:

function Alt() {
    this.a = 1;
    this.b = "2";
    this.c = [];
}
JSON.stringify(obj);

{"a":1,"b":"2","c":["3"]}

What's going on?

Example here: http://jsfiddle.net/FdzB6/

11
  • 2
    The JSON serializer only pays attention to properties directly on the objects being serialized ("own" properties). Commented Feb 12, 2014 at 15:34
  • 1
    @MikeEmery for "Foo", the function?!? No, the function "Foo" doesn't have those properties, and neither will an object created by calling new Foo - they'll be readable from the prototype via an instance, of course, but a hasOwnProperty("a") test will return false. Commented Feb 12, 2014 at 15:49
  • 1
    @RocketHazmat yes; there's really no difference between a simple string or number-valued prototype property and a function property. The lookup process for a property reference is the same. I should add an answer I guess :) Commented Feb 12, 2014 at 15:50
  • 1
    For reference, here's how is implemented Crockford's stringify : github.com/douglascrockford/JSON-js/blob/master/json2.js#L326 Commented Feb 12, 2014 at 15:51
  • 1
    @MikeEmery well, it depends on what you're trying to do. If the ability to serialize objects with JSON is really important, then yes I'd say it's a bad idea :) Commented Feb 12, 2014 at 16:10

2 Answers 2

3

Properties on an object's prototype (that is, the prototype of its constructor) are readable via a reference to an the object:

function Constructor() { }
Constructor.prototype.a = "hello world";

var x = new Constructor();
alert(x.a); // "hello world"

However, those properties really are "stuck" on the prototype object:

alert(x.hasOwnProperty("a")); // false

The JSON serializer only pays attention to properties that directly appear on objects being processed. That's kind-of painful, but it makes a little sense if you think about the reverse process: you certainly don't want JSON.parse() to put properties back onto a prototype (which would be pretty tricky anyway).

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

1 Comment

Thanks for this explanation - the bit about using JSON.parse later is especially helpful.
1

your answer lies Why is JSON.stringify not serializing prototype values?

JSON.stringify only does "own" properties.

In your first example: prototype members are being set, and then calling Stringify on the object itself, which does not have its own properties.

In your second: this.a will climb the chain until it finds the property

In the third: You are setting properties directly on the object and not its prototype

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.