Loading ...
Sorry, an error occurred while loading the content.

JSON with Existing Objects

Expand Messages
  • RouteSlip.com
    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
    Message 1 of 2 , Mar 23, 2006
    • 0 Attachment
      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?
    • Douglas Crockford
      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 ||
      Message 2 of 2 , Mar 23, 2006
      • 0 Attachment
        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?
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.