[問題] 有關JavaScript繼承問題
奇摩知識好讀版
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
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):