我创建了对象lik
e this
testObj.prototype = {
cVar: 15,
init: function(c){
/*initialization code*/
this.cVar = c;
}
};
var a = new testObj(10);
var b = new testObj(20);
现在,两个对象的cVar值均为20。它们是否共享变量?我如何为每个对象获取单独的变量?
最佳答案
是的,他们是共享的。对于单独的属性,请在构造函数中定义它们:
function Ctor() {
this.notShared = 1;
};
Ctor.prototype.shared = 2;
关于javascript - 原型(prototype)中定义的变量是否跨对象共享?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4022122/