routeslip@gmail.com wrote:
[color=blue]
> 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.[/color]
With JSON I think all you can do is pass e.g.
var obj = { 'prop1': 1, 'prop2': 2, 'prop3': 3, 'prop4': 4 }
around, you could iterate over the property names then and initialize
your "class2 instance"
var c2 = new class2();
for (var propertyName in obj) {
c2[propertyName] = obj[propertyName];
}
And for..in might enumerate properties added to Object.prototype so
depending on libraries you might use you need to take care with that
construct not to add properties defined on the prototype.
--
Martin Honnen
http://JavaScript.FAQTs.com/