variable defined in prototype are shared accross objects?
I have create object lik
e this
testObj.prototype = {
cVar: 15,
init: function(c){
开发者_如何学C /*initialization code*/
this.cVar = c;
}
};
var a = new testObj(10);
var b = new testObj(20);
Now both object's cVar values is 20. Are they sharing the variable? How i can get seperate variable for each object?
Yes, they're shared. For separate properties, define them inside the constructor:
function Ctor() {
this.notShared = 1;
};
Ctor.prototype.shared = 2;
精彩评论