这似乎是不一致的,但可能是因为我对JavaScript中的原型继承特性不熟悉。基本上,我有两个基类属性,“list”和“name”。我实例化了两个子类并给属性赋值。当我实例化第二个子类时,它从第一个子类实例获取了“list”值,但没有获取“name”值。到底发生了什么?当然,我希望任何后续的子类实例都不会从其他实例获取值,但如果发生了这种情况,它应该是一致的!
以下是代码片段:
function A()
{
this.list = [];
this.name = "A";
}
function B()
{
}
B.prototype = new A();
var obj1 = new B();
obj1.list.push("From obj1");
obj1.name = "obj1";
var obj2 = new B();
//output:
//Obj2 list has 1 items and name is A
//so, the name property of A did not get replicated, but the list did
alert("Obj2 list has " + obj2.list.length + " items and name is "+ obj2.name);