Instancing local objects using JSON notation

Discussion in 'Javascript' started by Zoran Perak, Oct 13, 2006.

  1. Zoran Perak

    Zoran Perak Guest

    Hi,

    I have a problem defining member as instance of local object (function) when
    defining object using JSON format.
    Here is an example:

    === code ===
    var Abc = function(a, b, c) {
    this.a = a;
    this.b = b;
    this.c = c;
    }

    var obj = {
    Xyz : function(a, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
    },
    CONST : 'some value',

    member1 : new Abc(1, 2, 3), // <-- OK
    member2 : new this.Xyz(1, 2, 3), // <-- error: this.Xyz is not constructor
    (this==window, not obj)
    prop1 : CONST // <-- error: CONST is not defined (how to reference
    obj.CONST here?)

    member3 : function() {
    var x = new this.Xyz(1, 2, 3); // <-- OK
    }
    }
    === /code ===

    I can define local member (member1) as new global object (Abc) but I would
    like to define it as new local object (obj.Xyz).
    I can instance local object inside code block (see member3) but I would like
    to create local object as member (see member2).

    Can someone tell me how to do this or in other words how to reference parent
    object (obj) while defining members ('this' in 'member2' definition
    references to global 'window' object)?

    Thanks
    Zoran Perak
     
    Zoran Perak, Oct 13, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grey Squirrel

    Hungarian Notation Vs. Pascal Notation?

    Grey Squirrel, Mar 19, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,316
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 21, 2007
  2. Immocation

    Instancing

    Immocation, Jul 6, 2007, in forum: VHDL
    Replies:
    0
    Views:
    502
    Immocation
    Jul 6, 2007
  3. crjjrc
    Replies:
    1
    Views:
    306
    Victor Bazarov
    Mar 21, 2008
  4. Tameem
    Replies:
    454
    Views:
    12,027
  5. KyleC
    Replies:
    2
    Views:
    122
    KyleC
    Oct 11, 2005
Loading...

Share This Page