tags:

views:

18

answers:

1

I have create object 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);

Now both object's cVar values is 20. Are they sharing the variable? How i can get seperate variable for each object?

+2  A: 

Yes, they're shared. For separate properties, define them inside the constructor:

function Ctor() {
    this.notShared = 1;
};

Ctor.prototype.shared = 2;
Ionuț G. Stan
how to access this.notShared? inside prototype.init?
coure06
`this.notShared`, but you must call the constructor before calling `init`.
Ionuț G. Stan