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

I

iporter

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?
 
H

Henry

On Oct 18, 2:39 pm, iporter wrote:
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.

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.
 
R

RobG

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
 
D

dhtmlkitchen

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
 
R

RobG

On Oct 19, 5:05 pm, "(e-mail address removed)" <[email protected]>
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'."
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top