Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

json · JSON JavaScript Object Notation

The Yahoo! Groups Product Blog

Check it out!

Group Information

  • Members: 593
  • Category: Data Formats
  • Founded: Jul 19, 2005
  • Language: English
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Messages

Advanced
Messages Help
JSON with Existing Objects   Topic List   < Prev Topic  |  Next Topic >
Summarize Messages Sort by Date  
#305 From: RouteSlip.com <routeslip@...>
Date: Thu Mar 23, 2006 9:18 pm
Subject: JSON with Existing Objects
ryanhca
Send Email Send Email
 
Hi. I'm pretty experienced with Javscript but totally new to JSON and
have a question...

Is it possible to use JSON to "populate" objects that are already
defined? For instance, I have two "classes":

function class1() {
this.class2_array;
}

function class2() {
this.prop1;
this.prop2;
this.prop3;
this.prop4;
}

I want to create a bunch of class2 objects in the class1.class2_array
property.

The existing classes already have their own methods, etc so it's not
just about storing data. Previously I created log signatures for class2
and basically created the objects the "old fashioned" way.

Can I do this?



#306 From: "Douglas Crockford" <douglas@...>
Date: Thu Mar 23, 2006 9:38 pm
Subject: Re: JSON with Existing Objects
douglascrock...
Send Email Send Email
 
There are a number of approaches. An elegant one is to pass an
initialization object to the constructor.

function class2(init) {
this.prop1 = init.prop1 || default_prop1;
...
}

The constructor can do checking on the initial values, filling in
defaults.

With a constructor in this style, you simply pass it the object that
JSON produced. You could also wrap it so that it gets delivered to the
right constructor:

foo = {
"class": "class2",
"init": {
"prop1": ...
}
};

new window[foo["class"]](foo.init);

--- In json@yahoogroups.com, RouteSlip.com <routeslip@...> wrote:
>
> Hi. I'm pretty experienced with Javscript but totally new to JSON and
> have a question...
>
> Is it possible to use JSON to "populate" objects that are already
> defined? For instance, I have two "classes":
>
> function class1() {
> this.class2_array;
> }
>
> function class2() {
> this.prop1;
> this.prop2;
> this.prop3;
> this.prop4;
> }
>
> I want to create a bunch of class2 objects in the class1.class2_array
> property.
>
> The existing classes already have their own methods, etc so it's not
> just about storing data. Previously I created log signatures for class2
> and basically created the objects the "old fashioned" way.
>
> Can I do this?
>






 
Add to My Yahoo!      XML What's This?

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines NEW - Help