[問題] 有關JavaScript繼承問題

看板Web_Design作者 (Chansamo)時間14年前 (2012/02/18 13:15), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/2 (看更多)
奇摩知識好讀版 http://ppt.cc/klRb 程式碼如下, function GrandFather(){ this.g1=""; this.g2=""; }; function Father(){ this.p3=""; this.p4=""; }; function Child(){ this.c5=c5; }; Father.prototype=new GrandFather(); Child.prototype=new Father(); document.write(Child.prototype.constructor); document.write(Child.prototype.prototype.constructor); 現在我正在練習javascript的繼承問題, 目前是想做兩層的繼承 雖然單從繼承屬性空間的概念上來說, 繼承的結果是成功的, 但依照原型鏈以及下面 Father.prototype=new GrandFather(); Child.prototype=new Father(); 這兩行來看, 若我執行 document.write(Child.prototype.constructor);\\Father function(詳細構造器) document.write(Child.prototype.prototype.constructor);\\GrandFather function( 詳細構造器) 結果應該是分別指向 但真正的執行結果卻是 document.write(Child.prototype.constructor);\\GrandFather function(詳細構造器) document.write(Child.prototype.prototype.constructor);\\錯誤 後來我查firefox的firbug查詢結果如下圖 http://ppt.cc/PuJ2 其指向之父類別皆為GrandFather, 為何Child的第一層prototype沒有指向Father而是指向GrandFather呢? 以及若要修正應該要如何實做呢? 還請各位大大賜教。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.244.176.30
文章代碼(AID): #1FFpFfQ9 (Web_Design)
文章代碼(AID): #1FFpFfQ9 (Web_Design)