I am doing Object Oriented programming in JavaScript without Prototype/jQuery (I use jQuery for other stuff). It has been working fine so far, but I ran into an issue with inheritance. Basically, when I declare objects in a constructor, they are shared between instances. Below is some sample code:
A = function()
{
this.y = new Array();
}
A.prototype.doStuff = function(n)
{
this.y.push(n);
}
B = function()
{
}
B.prototype = new A();
var b1 = new B();
var b2 = new B();
b1.doStuff(100);
b2.doStuff(200);
console.log("b1:");
console.log(b1);
console.log("b2:");
console.log(b2);
This outputs an array [100, 200]
for both b1
and b2
. What I want is for b1
and b2
to have their own, separate arrays for y
. How do I go about this?
(PS. I assume that Prototype's class system has something built in for this. However I would rather not rewrite a bunch of my code to use that class system)