作者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