W
Will
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
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