(I'm new to JavaScript). The following code:
function A() {
console.log('Constructing A');
this.a = new Array();
}
function B(x) {
console.log('Constructing B');
this.a.push(x);
this.b = x;
}
B.prototype = new A();
b1 = new B(10);
b2 = new B(11);
console.log('b1', b1);
console.log('b2', b2);
Results in b1 and b2 sharing single this
.a array (but different this.b
). It's like a shallow copy.
I don't quite understand what is the right way to create separate this.a
arrays. I want them inherited because this is the logic of the code, besides I don't want to create them in each and every child object (and there are many child objects in my case).