function SuperType(){
this.name='s;'
this.colors = ["red", "blue", "green"];
}
function SubType(){
}
//inherit from SuperType
SubType.prototype = new SuperType();
var instance1 = new SubType();
var instance2 = new SubType();
instance1.name='ssss';
instance1.colors=["red", "blue"];
alert(instance1.colors); //"red,blue,green,black"
alert(instance1.name); //"ssss"
alert(instance2.colors); //"red,blue,green,black"
alert(instance2.name); //"s"
instance2.name 为什么没有改变
function SuperType(){
this.name='sdsd';
this.colors = ["red", "blue", "green"];
}
function SubType(){
//inherit from SuperType
SuperType.call(this);
}
var instance1 = new SubType();
instance1.colors.name='aaaaaaa';
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
alert(instance1.name); //"a"
var instance2 = new SubType();
instance2.colors.name='bbbbbbb';
alert(instance2.colors); //"red,blue,green"
alert(instance2.name); //"a"
instance2.name和instance1.name 都没有改变
console.log(a.key)
)和写(a.key = 1
)的时候是不一样的,读的时候会一直查下去,写的时候发现本身没有就直接创建赋值了。{name, colors}
。那你改对colors添加name是不会影响到name属性的。