OOP, object literal notation, and the 'new' operator - how to have multiple child objects of a paren

Discussion in 'Javascript' started by iporter, Oct 18, 2007.

  1. iporter

    iporter Guest

    I wonder if anyone can clear up an OOP issue for me, specifically, how
    to have multiple child objects of a parent object. Consider the code
    below:

    var parentObj={
    childCount: 0,
    childObj: {
    id:false,
    init: function() {
    alert(this.id);
    parentObj.childCount++;
    this.id = parentObj.childCount;
    alert("Child " + this.id + " Created");
    }
    }
    }

    Calling 'parentObj.childObj.init();', the first alert produces
    'undefined' and the second 'Child 1 Created'. A second call to
    'parentObj.childObj.init();' produces the '1' from above instead of
    the 'undefined' I'm expecting - I realise this is because I'm working
    with the same object.

    However, calling 'var firstChild = parentObj.childObj.init(); var
    secondChild = parentObj.childObj.init()' produces the same result (as
    does 'var firstChild = parentObj.childObj; firstChild.init();).

    I've also tried the 'new' operator. But the code 'var firstChild =
    new parentObj.childObj;' produces the error 'parent.childObjis not a
    constructor'.

    Thus, you can see I'm missing the point - can anyone point me in the
    right direction?
     
    iporter, Oct 18, 2007
    #1
    1. Advertising

  2. iporter

    Henry Guest

    On Oct 18, 2:39 pm, iporter wrote:
    <snip>
    > var parentObj={
    > childCount: 0,
    > childObj: {
    > id:false,
    > init: function() {
    > alert(this.id);
    > parentObj.childCount++;
    > this.id = parentObj.childCount;
    > alert("Child " + this.id + " Created");
    > }
    > }
    >
    > }
    >
    > Calling 'parentObj.childObj.init();', the first alert
    > produces 'undefined' ...


    This description is either false or omitting significant context
    information.

    <snip>
    > Thus, you can see I'm missing the point - can anyone point
    > me in the right direction?


    You have not explained what you are trying to acheave.
     
    Henry, Oct 18, 2007
    #2
    1. Advertising

  3. iporter

    RobG Guest

    On Oct 18, 11:39 pm, iporter <> wrote:
    > I wonder if anyone can clear up an OOP issue for me, specifically, how
    > to have multiple child objects of a parent object. Consider the code
    > below:
    >
    > var parentObj={
    > childCount: 0,
    > childObj: {
    > id:false,
    > init: function() {
    > alert(this.id);


    When init is called as a method of childObj, the function's this
    keyword will be set as a reference to childObj.

    > parentObj.childCount++;
    > this.id = parentObj.childCount;


    And here you set childObj.id to the value of the recently incremented
    childCount.


    > alert("Child " + this.id + " Created");
    > }
    > }
    > }
    >
    > Calling 'parentObj.childObj.init();', the first alert produces
    > 'undefined'


    For me it shows false in both IE and Firefox.

    > and the second 'Child 1 Created'. A second call to
    > 'parentObj.childObj.init();' produces the '1' from above instead of
    > the 'undefined' I'm expecting - I realise this is because I'm working
    > with the same object.


    Why would you expect it to be 'undefined' when a direct consequence of
    calling parentObj.childObj.init is to set the id property of
    parentObj.childObj to parentObj.childCount, which you increment each
    time the init function is called?


    > However, calling 'var firstChild = parentObj.childObj.init(); var
    > secondChild = parentObj.childObj.init()'


    Given that the function doesn't return anything, the value of
    firstChild and secondChild will be undefined.

    > produces the same result (as
    > does 'var firstChild = parentObj.childObj; firstChild.init();).


    Which result is that? The result of calling the function twice will
    not change just because you assign the results to different variables.


    > I've also tried the 'new' operator. But the code 'var firstChild =
    > new parentObj.childObj;' produces the error 'parent.childObjis not a
    > constructor'.


    Precisely. The new operator expects to be used with a function, not a
    plain object.


    > Thus, you can see I'm missing the point - can anyone point me in the
    > right direction?


    If I had any idea what that might be I'd try, hopefully the above will
    help you to ask a question that can be answered more precisely. :)

    It seems to me that your issue is not creating an object structure,
    but understanding how the value of a function's this keyword is set.
    Try this thread:

    <URL:
    http://groups.google.com.au/group/c...=this keyword michael winter#7134df52136d6a13
    >




    --
    Rob
     
    RobG, Oct 19, 2007
    #3
  4. iporter

    Guest

    On Oct 18, 8:56 pm, RobG <> wrote:
    > On Oct 18, 11:39 pm, iporter <> wrote:
    >
    > > I wonder if anyone can clear up an OOP issue for me, specifically, how
    > > to have multiple child objects of a parent object. Consider the code
    > > below:

    >
    > > var parentObj={
    > > childCount: 0,
    > > childObj: {
    > > id:false,
    > > init: function() {
    > > alert(this.id);

    >
    > When init is called as a method of childObj, the function's this
    > keyword will be set as a reference to childObj.
    >

    Yep.



    var anInit = parentObj.childObj.init(); // childObj has been modified.
    anInit == undefined;

    But what is happening when you use new?

    var anInit = new parentObj.childObj.init();
    anInit instanceof parentObj.childObj.init;

    Garrett

    > --
    > Rob
     
    , Oct 19, 2007
    #4
  5. iporter

    RobG Guest

    On Oct 19, 5:05 pm, "" <>
    wrote:
    [...]
    > But what is happening when you use new?
    >
    > var anInit = new parentObj.childObj.init();
    > anInit instanceof parentObj.childObj.init;


    That is not what the OP posted, the original is:

    "...the code 'var firstChild =
    new parentObj.childObj;' produces the error
    'parent.childObjis not a constructor'."


    --
    Rob
     
    RobG, Oct 19, 2007
    #5
    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. Curts
    Replies:
    1
    Views:
    340
    Lee Fesperman
    Aug 21, 2003
  2. Grey Squirrel

    Hungarian Notation Vs. Pascal Notation?

    Grey Squirrel, Mar 19, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,364
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 21, 2007
  3. Josuan
    Replies:
    0
    Views:
    296
    Josuan
    Jun 2, 2008
  4. Tameem
    Replies:
    454
    Views:
    12,498
  5. Robert Mark Bram

    Dot notation V Bracket notation

    Robert Mark Bram, Jul 4, 2003, in forum: Javascript
    Replies:
    3
    Views:
    484
    Robert Mark Bram
    Jul 5, 2003
Loading...

Share This Page