473,322 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Prototyping - explanation required

function obj1() {
this.children;

this.Children = function() {
if(typeof(this.children) === 'undefined') {
this.children = new col();
}
return this.children;
}

this.Children().Add(1);
}

obj2.prototype = new obj1();
function obj2() {
obj1.call(this);
this.Children().Add(1);
}

obj3.prototype = new obj2();
function obj3() {
obj2.call(this);
this.Children().Add(1);
}

function col() {
var arr = new Array();

this.Add = function(item) {
arr[arr.length] = item;
}

this.Count = function(item) {
return arr.length;
}
}

var a = new obj3();
alert(a.Children().Count());

I have been learning how to use prototyping with private static
members and today noticed something new to me. In the above example, 6
is returned when 3 would be desirable.

Turning the public member "this.children" into a private member (e.g.
"var children") corrects the issue, as does removing the lines
"obj1.call(this)" and "obj2.call(this)".

Am I correct in deducing that any public members in a prototyped
object will be re-evaluated for each derived object? So I'm guessing
that in this case, when obj3 is instantiated:

1) obj1, obj2 and obj3 all call children.Add() in the constructor
(Count = 3)
2) obj1.call(this) causes obj1 to call children.Add() again
3) obj2.call(this) causes obj2 to call children.Add() again, and also
causes a repeat of (2).

As a result children.Add() is called 6 times instead of 3.

Can anyone explain the above behaviour or correct my interpretation? I
am curious to know what xactly is happening and how to work around it.

Thanks

Will
Jul 20 '05 #1
1 2360
mr**********@yahoo.com (Will) writes:
function obj1() {
this.children;
This line does nothing (except perhaps evaluating to undefined).
I assume it was originally a "var children" line :)
this.Children = function() {
if(typeof(this.children) === 'undefined') {
(you only need == for string comparison. If you wrote
if(this.children === undefined)
i.e., compare to the value itself, not a string, then the === is
needed. Still, better safe than sorry :)
this.children = new col();
}
return this.children;
}

this.Children().Add(1);
} obj2.prototype = new obj1();
function obj2() {
I find it slightly confuzing to set a property of something that is
not declared until the next line. I know ECMAScript declares all functions
before executing any code, but it is still ... unsetteling.
obj1.call(this);
this.Children().Add(1);
}

obj3.prototype = new obj2();
function obj3() {
obj2.call(this);
this.Children().Add(1);
}

function col() {
var arr = new Array();

this.Add = function(item) {
arr[arr.length] = item;
}

this.Count = function(item) {
return arr.length;
}
}

var a = new obj3();
alert(a.Children().Count());

I have been learning how to use prototyping with private static
members and today noticed something new to me. In the above example, 6
is returned when 3 would be desirable. Turning the public member "this.children" into a private member (e.g.
"var children") corrects the issue, as does removing the lines
"obj1.call(this)" and "obj2.call(this)".
Yes. If you change children to a local variable of the obj1 function,
then the two later calls of obj1 will create Children functions that
operate on the new variable. So each new obj2 object will get its own
Children function and children variable.

If this.children is a property, the new Children function assigned
to obj2 objects will all refer to the same property of the obj1 object
that is obj2's prototype. Only one of them will find it undefined.

Removing the calls to obj1 and obj2 should lower the count, since they
perform three of the Add(1)'s.
Am I correct in deducing that any public members in a prototyped
object will be re-evaluated for each derived object?
I am not sure exactly what you mean, but I think the answer is no.
Members (i.e., properties of objects) are not evaluated at all, they
just sit there until you change them.
So I'm guessing that in this case, when obj3 is instantiated:

1) obj1, obj2 and obj3 all call children.Add() in the constructor
(Count = 3)
2) obj1.call(this) causes obj1 to call children.Add() again
3) obj2.call(this) causes obj2 to call children.Add() again, and also
causes a repeat of (2).
Let me try to figure out what happens. Then we can compare :)
(And after doing that, yes, I agree. Derivation follows:)

Some functions are defined. Then the following lines are executed:

obj2.prototype = new obj1();
This instantiates obj1instance.children and adds 1 to it.
State after this:
[obj2]--prototype-->[obj1ins:col(Count=1)]
(i.e., the obj2 function has a property called "prototype" that points
to an instans of obj1 with a collection with Count()==1)

obj3.prototype = new obj2();
This creates obj3 and an obj2 instance:
[obj3]--prototype-->[obj2ins]--<<proto>>-->[obj1ins:col(Count=1)]
This also executes the obj2 function:
obj1.call(this);
It creates a new this.Children function, but not a new children collection.
It then adds one to the collection.
this.Children().Add(1);
It adds another one.
State:
[obj3]--prototype-->[obj2ins]--<<proto>>-->[obj1ins:col(Count=3)]
Then we execute:
var a = new obj3();
This creates a new obj3 instance:
a = [obj3ins]--<<proto>>-->[obj2ins]--<<proto>>-->[obj1ins:col(Count=3)]
and executes the obj3 function:
obj2.call(this);
this.Children().Add(1);
The first call executes obj2:
obj1.call(this);
this.Children().Add(1);
The first call again executes obj1, which creates a new Children function
and does:
this.Children().Add(1);
That is, a total of three calls to Add(1), bringin us to a count of 6.
In short, if we show the unfolded function calls indented, the
code that is executed is:

obj2.prototype = new obj1();
this.children = new col();
this.Children().Add(1);
obj3.prototype = new obj2();
obj1.call(this);
this.Children().Add(1);
this.Children().Add(1);
a = new obj3();
obj2.call(this);
obj1.call(this);
this.Children().Add(1);
this.Children().Add(1);
this.Children.Add(1);
As a result children.Add() is called 6 times instead of 3.
I can't see why it should only be 3. You explicitly call functions
that Add(1). Nothing automatic is happening.
Can anyone explain the above behaviour or correct my interpretation? I
am curious to know what xactly is happening and how to work around it.


I hope this is readable. :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Dave Allison | last post by:
Oh no, not another "check out my cool new language" posting :-) For about 5 years now, I have been developing a scripting/prototyping language that is now available on the net. It's called...
1
by: cainlevy | last post by:
Hey all, What are the pros and cons of defining methods in the constructor vs through the prototype? For example: Constructing: ------------- function MyObj() { this.MyMethod = function()...
1
by: jimfortune | last post by:
From: http://groups-beta.google.com/group/comp.databases.ms-access/msg/769e67e3d0f97a90?hl=en& Errata: 19 solar years = 2939.6018 days should be 19 solar years = 6939.6018 days Easter...
13
by: rs | last post by:
Dear All, I have a question regarding proptypes for functions. What is the recommended practice? The way I do it is to put all my external functions in a header file, while protyping...
6
by: Buck Rogers | last post by:
Hi guys! Love your work! The below program is from K&R2, p22. ================================= #include <stdio.h> /* count digits, white space, others */ main() {
70
by: rahul8143 | last post by:
hello, 1) First how following program get executed i mean how output is printed and also why following program gives different output in Turbo C++ compiler and Visual c++ 6 compiler? void main()...
4
by: Tilted | last post by:
Does anyone here use prototyping tools? I'm building one myself as I feel like I'm doing the same thing over and over again with the majority of my projects, how do people generally feel about...
14
by: Akhil | last post by:
plz c d following code #include<stdio.h> void main() { int x=4,y=1; y=x++++; //gives error message lvalue required y=x++ + ++y;//no errors
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.