(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 the same this.a array (but different this.b). It is like a shallow copy.
I do not quite understand how to create separate arrays correctly this.a. I want them to be inherited, because this is the logic of the code, but I do not want to create them in every child (and in my case a lot of children).
source
share