function Parent(firstName,color){
this.firstName = firstName;
this.color = color;
this.showName = function (){
console.log("我家姓氏:"+this.firstName);
}
}
Parent.prototype.showAll=function(){
console.log("姓:"+this.firstName+"\n"+"喜爱的颜色:"+this.color);
}
function Child(myName,age,firstName,color){
this.myName = myName;
this.age = age;
Child.prototype=Parent.prototype;
Parent.call(this,firstName,color);
}
/*Child.prototype=Parent.prototype;*/
var c = new Child("帅",23,"孙","粉色");
var d=new Parent("孙","粉色");
d.showAll();//姓:孙
//喜爱的颜色:粉色
c.showAll();//error c.showAll is not a function
call对象冒充为什么this不能获得构造函数Parent的原型?还有Child.prototype=Parent.prototype;写在Child函数里不行,为什么拿出来写在window环境就可以?
题主:代码可以再捋一捋
第一个问题:
this
不能获得构造函数Parent
的原型第二个问题:写在
Child
函数里不行,为什么拿出来写在window
环境就可以?