87,989
社区成员
发帖
与我相关
我的任务
分享 function Parent(){
this.a = 1;
this.b = [1,2,this.a];
this.c = { demo : 5 };
this.show = function (){
console.log( this.a , this.b , this.c.demo );
}
}
function Child(){
this.a = 2;
this.change = function(){
this.b.push(this.a);
this.a = this.b.length;
this.c.demo = this.a++;
}
}
Child.prototype = new Parent();
var parent = new Parent();
var child1 = new Child();
var child2 = new Child();
child1.a = 11;
child2.a = 12;
parent.show();
child1.show();
child2.show();
child1.change();
child2.change();
parent.show();
child1.show();
child2.show();
[/quote]
a是非引用类型,所以只会修改自己的实例值,b是数组,引用类型,所以修改后会反应到所有的实例中,其实就是
Child的prototype是同一个Parent对象,所以引用类型属性的更改都会反映到所有Child实例,非引用类型属性只会修改对应实例值
这句,你好好理解下