(私はJavaScriptが初めてです)。次のコード:
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);
結果として、b1 と b2 は単一の this
.a 配列を共有します (ただし、 は異なります this.b
)。浅いコピーのようなものです。
個別の this.a
配列を作成する正しい方法が何であるかがよくわかりません。これはコードのロジックであるため、継承したいのですが、すべての子オブジェクトでそれらを作成したくないことに加えて (私の場合は多くの子オブジェクトがあります)。