> function Parent() {this.arr = [];}
undefined
> function Child() {}
undefined
> Child.prototype = new Parent();
{ arr: [] }
> child1 = new Child();
{}
> child2 = new Child();
{}
> child1.arr.push('From child1');
1
> child2.arr
[ 'From child1' ]
>
鑑於上述,我期望child2.arr
是空的,因爲它是它自己的對象。我如何讓child1和child2包含他們自己的arr?謝謝!繼承的子對象在JavaScript中共享相同的數組屬性?
在子構造函數中添加賦值非常棒!謝謝! – hooinator