【发布时间】:2014-11-01 19:35:58
【问题描述】:
我试图从同一个类定义中获取两个对象。但是,它们似乎具有相同的属性。我能做什么?
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 我如何将其标为自己的财产?
标签: javascript class oop object