OOP, the new operator & object literal notation - multiple child objects of a parent

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.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 18th, 2007, 13:32
New Member
Join Date: Mar 2007
Location: Glasgow, Scotland
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Reply With Quote

  #2 (permalink)  
Old Oct 21st, 2007, 07:51
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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

Code: Select all
 
function parentObject (objectId)
{
    this.id = objectId;
    this.children = new Array();
}
 
parentObject.prototype.createChild = function ()
{
    this.children[this.children.length+1] = new childObject(this);
    alert('Child object number ' + this.children.length + ' created!');
}
 
function childObject (parentObj)
{
     this._parent = parentObj;
     this.childId = this._parent.childreb.length + 2;
}
 
var parent = new parentObject('theParent');
 
parentObject.createChild();parentObject.createdChild();parentObject.createChild();
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 00:04.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43