原型中定义的变量是否与对象共享?

cou*_*011 1 javascript prototype

我创建了对象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);
Run Code Online (Sandbox Code Playgroud)

现在两个对象的cVar值都是20.它们是否共享变量?我如何为每个对象获得单独的变量?

Ion*_*tan 6

是的,他们是共享的.对于单独的属性,在构造函数中定义它们:

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

Ctor.prototype.shared = 2;
Run Code Online (Sandbox Code Playgroud)