//在使用原型链进行继承一定要注意下面问题
//问题1.不能再设定原型链之后,再重新为原型链赋值
//问题2.一定要在原型链之后才能添加或者赋值
//使用原型链继承时,缺陷一:无法在子类中调用父类的构造方法
//缺陷二:如果父类中有引用类型,此时这个引用添加到子类的原型中
//当第一个修改后第二个也会被修改
function Parent(){
this.pv = "parent";
this.color = ["red", "blue"];
}
Parent.prototype.showParent = function(){
alert(this.pv);
}
function Child(){
this.cv = "child";
}
// 问题2演示:
// Child.prototype.showChild = function(){
// alert(this.cv);
// }
// 当使用以上方法,如果进行赋值之后才进行原型链的设定,这样赋值的原型对象就会被重写,
// 赋值的对象就不存在新的原型中
//让child原型链指向Parent对象,也就等于完成了继承
Child.prototype = new Parent();
Child.prototype.showChild = function(){
alert(this.cv);
}
//覆盖父类的方法
Child.prototype.showParent = function(){
alert("over parent");
}
//问题一演示
//Child.prototype = function(){
// alert(this.cv);
// }
//当使用以上方法时等于又重写了Child原型,这样就不存在任何继承关系了
var c1 = new Child();
c1.color.push("black");
alert(c1.color);
c1.showParent();
c1.showChild();
var c2 = new Child();
alert(c2.color);//缺陷二在这里体现,这里不应该出现新添加的black