我想從同一個類定義中獲取兩個對象。但是他們似乎共享相同的屬性。我能做什麼?JavaScript新不會創建新對象
http://jsfiddle.net/dagod/nuam8dks/2/
myclass = function() {
this.data.push(Math.random(1000));
};
myclass.prototype.data = [];
a = new myclass();
b = new myclass();
console.log(a.data);
console.log(b.data); //same as a.data
'data'是共享的,因爲你把它的原型。在構造函數中聲明'data'作爲自己的屬性。另外,使用'var'聲明你的變量,並且把你的構造函數作爲一個慣例來使用,所以你不要忘記'new'。 – elclanrs 2014-11-01 19:37:41
@elclanrs我將如何decalre它作爲自己的財產? – Merion 2014-11-01 19:38:39