This is a discussion on "OOP, the new operator & object literal notation - multiple child objects of a parent" within the JavaScript Forum section. This forum, and the thread "OOP, the new operator & object literal notation - multiple child objects of a parent are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
OOP, the new operator & object literal notation - multiple child objects of a parent
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
OOP, the new operator & object literal notation - multiple child objects of a parent
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? |
|
|
|
||||
|
Re: OOP, the new operator & object literal notation - multiple child objects of a par
I'm sorry, maybe because I'm tired, can you explaint what you mean in a bit more detail?
To create an object that has children you could use something like
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
|
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Child div expand to fit parent? | linchpin311 | Web Page Design | 5 | Apr 20th, 2008 21:16 |
| Javascript Parent and Child menu in Safari?? | TR123 | JavaScript Forum | 3 | Mar 8th, 2007 14:15 |
| Submitting a parent form from a child window in IE 7 | livehed | JavaScript Forum | 0 | Feb 15th, 2007 16:23 |
| parent child forms | mrproggie | ASP.NET Forum | 1 | Aug 12th, 2006 17:49 |
| Parent Child accesssing | greenkhan | ASP.NET Forum | 1 | Jun 20th, 2005 09:21 |