Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > OOP, object literal notation, and the 'new' operator - how to have multiple child objects of a parent object?

Reply
Thread Tools

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

 
 
iporter
Guest
Posts: n/a
 
      10-18-2007
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
 
 
 
 
Henry
Guest
Posts: n/a
 
      10-18-2007
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.

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      10-19-2007
On Oct 18, 11:39 pm, iporter <(E-Mail Removed)> 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/co...34df52136d6a13
>




--
Rob

 
Reply With Quote
 
dhtmlkitchen@gmail.com
Guest
Posts: n/a
 
      10-19-2007
On Oct 18, 8:56 pm, RobG <(E-Mail Removed)> wrote:
> On Oct 18, 11:39 pm, iporter <(E-Mail Removed)> 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



 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      10-19-2007
On Oct 19, 5:05 pm, "(E-Mail Removed)" <(E-Mail Removed)>
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

 
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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
child jitter vs parent shaping vs child class-default on 871 and 1841 Robertas Cisco 0 02-23-2007 01:28 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Pass from parent to child, then update parent with child value... Noel Dolan Javascript 0 07-18-2004 05:52 PM



Advertisments