Я пытаюсь определить класс javascript с свойством array и его подклассом. Проблема заключается в том, что все экземпляры подкласса как-то «акция» свойство массива:Наследование и массивы Javascript
// class Test
function Test() {
this.array = [];
this.number = 0;
}
Test.prototype.push = function() {
this.array.push('hello');
this.number = 100;
}
// class Test2 : Test
function Test2() {
}
Test2.prototype = new Test();
var a = new Test2();
a.push(); // push 'hello' into a.array
var b = new Test2();
alert(b.number); // b.number is 0 - that's OK
alert(b.array); // but b.array is containing 'hello' instead of being empty. why?
Как вы можете видеть, что я не имею эту проблему с примитивными типами данных ... Любые предложения?
См http://stackoverflow.com/questions/1485824/javascript -inheritance-objects-declaration-in-constructor-are-shared-between-inst –