M
Marcin
Hi everyone,
What I'm trying to achieve with the below code is
1. create a base 'class', called List
2. derive a 'class' from it called AList
3. derive another 'class' from AList, called BList, and then create
instances of both AList and BList()
The problem I have is that at step 3, when I create instances, the two
instances seem to share the same data, incorrectly. The blist()
variable at the end hasn't had anything added to it, yet it contains
what was added to the alist().
Can anyone could explain to me what I'm doing wrong ( tried something
like this on an ASP server running JScript, and using Firefox and both
give me the same results, so I'm guessing I'm misunderstanding this).
Thanks in advance
-Marcin
function print(str) {
console.log(str + "<br>");
}
var List = function() {
this.items = [];
}
List.prototype.add = function( num ) {
this.items.push(num);
return this;
}
List.prototype.toString = function() {
return this.items.join(" -- ");
}
var AList = function() {
}
AList.prototype = new List();
var BList = function() {
}
BList.prototype = new AList();
var alist = new AList();
alist.add(2);
alist.add(4);
print("alist is " + alist); //a list should have 2 and 4 printed out
var blist = new BList();
print("blist is " + blist); //b list shouldn't have anything, but it
does.
What I'm trying to achieve with the below code is
1. create a base 'class', called List
2. derive a 'class' from it called AList
3. derive another 'class' from AList, called BList, and then create
instances of both AList and BList()
The problem I have is that at step 3, when I create instances, the two
instances seem to share the same data, incorrectly. The blist()
variable at the end hasn't had anything added to it, yet it contains
what was added to the alist().
Can anyone could explain to me what I'm doing wrong ( tried something
like this on an ASP server running JScript, and using Firefox and both
give me the same results, so I'm guessing I'm misunderstanding this).
Thanks in advance
-Marcin
function print(str) {
console.log(str + "<br>");
}
var List = function() {
this.items = [];
}
List.prototype.add = function( num ) {
this.items.push(num);
return this;
}
List.prototype.toString = function() {
return this.items.join(" -- ");
}
var AList = function() {
}
AList.prototype = new List();
var BList = function() {
}
BList.prototype = new AList();
var alist = new AList();
alist.add(2);
alist.add(4);
print("alist is " + alist); //a list should have 2 and 4 printed out
var blist = new BList();
print("blist is " + blist); //b list shouldn't have anything, but it
does.