第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

求高手回答原型繼承的問(wèn)題

求高手回答原型繼承的問(wèn)題

眼眸繁星 2018-11-13 17:19:57
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"+"喜愛(ài)的顏色:"+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();//姓:孫            //喜愛(ài)的顏色:粉色c.showAll();//error c.showAll is not a functioncall對(duì)象冒充為什么this不能獲得構(gòu)造函數(shù)Parent的原型?還有Child.prototype=Parent.prototype;寫(xiě)在Child函數(shù)里不行,為什么拿出來(lái)寫(xiě)在window環(huán)境就可以?
查看完整描述

1 回答

?
尚方寶劍之說(shuō)

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊

題主:代碼可以再捋一捋

Child.prototype=Parent.prototype    // Parent.prototype指向的是Object

Child.prototype=Parent    //該是這樣吧

第一個(gè)問(wèn)題:this不能獲得構(gòu)造函數(shù)Parent的原型

function Child() 定義一個(gè)函數(shù),使用var c = new Child() this的指向?yàn)閷?duì)象c,就沒(méi)指向 Parent,不過(guò)你使用c.showName(),還是能獲取到的,因?yàn)?code>原型繼承.

第二個(gè)問(wèn)題:寫(xiě)在Child函數(shù)里不行,為什么拿出來(lái)寫(xiě)在window環(huán)境就可以?

寫(xiě)在函數(shù)里面也行的, this.prototype=Parent;
一個(gè)new做了以下幾件事情:

  1. 創(chuàng)建一個(gè)新的對(duì)象

  2. 查找Childprototype上的所有方法、屬性,復(fù)制一份給創(chuàng)建的對(duì)象

  3. 將構(gòu)造函數(shù)Child內(nèi)部的this指向創(chuàng)建的對(duì)象

  4. 創(chuàng)建的對(duì)象的__proto__指向Childprototype

  5. 執(zhí)行構(gòu)造函數(shù)

  6. 返回新創(chuàng)建的對(duì)象給變量c

寫(xiě)在內(nèi)部就是在第五部寫(xiě)入,寫(xiě)在外部是在第四部寫(xiě)入,故與內(nèi)外無(wú)關(guān)。


查看完整回答
反對(duì) 回復(fù) 2018-12-05
  • 1 回答
  • 0 關(guān)注
  • 443 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)