javascript - 求高手回答原型继承的问题
大家讲道理
大家讲道理 2017-04-11 13:11:23
0
2
166
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环境就可以?
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回覆(2)
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!