作者Chansamo (Chansamo)
看板Ajax
標題Fw: [問題] 有關JavaScript繼承問題
時間Sat Feb 18 13:21:04 2012
※ [本文轉錄自 Web_Design 看板 #1FFpFfQ9 ]
作者: Chansamo (Chansamo) 看板: Web_Design
標題: [問題] 有關JavaScript繼承問題
時間: Sat Feb 18 13:15:18 2012
奇摩知識好讀版
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
※ 發信站: 批踢踢實業坊(ptt.cc)
※ 轉錄者: Chansamo (60.244.176.30), 時間: 02/18/2012 13:21:04