首页 新闻 会员 周边

javascript怎么继承不了呀????打印为underfined呀??

0
悬赏园豆:15 [已解决问题] 解决于 2012-04-09 11:00

<script language="javascript" type="text/javascript">
function All(border){
this.border=border;
}
All.prototype.getera=function (){
return -1;
}
/*
三角形构造
*/
function Triangle(height,width){
All.call(this,3)
this.height=height;
this.width=width;
}
Triangle.prototype.getera=function(){
return this.height*this.width/2;
}
Triangle.prototype.getBorder=function(){
return this.border;
}

/*
四边形构造
*/
function linXing(height,width){

Triangle.call(this,height,width);
}
linXing.prototype.getera=function(){
return this.height*this.width;
}

/*
var t=new Triangle(20,4);
alert(t.getera());
alert(t.getBorder());
*/

var m=new linXing(20,4);
document.write(m.getera());
document.write(typeof m.getBorder);


</script>

问题补充:

我想问的是为什么可以继承getera这个属性,但是继承不了getBorder这个属性呀??????

unbreakable的主页 unbreakable | 初学一级 | 园豆:111
提问于:2012-04-09 10:20
< >
分享
最佳答案
0
/*
四边形构造
*/
function linXing(height,width){
Triangle.call(this,height,width);
}
linXing.prototype.getera=function(){
return this.height*this.width;
}

这里添加一行代码:

/*
四边形构造
*/
function linXing(height,width){
Triangle.call(this,height,width);
}

//添加一行代码
linXing.prototype=new Triangle();

linXing.prototype.getera=function(){
return this.height*this.width;
}
收获园豆:15
artwl | 专家六级 |园豆:16736 | 2012-04-09 10:37

我想问的是为什么可以继承getera这个属性,但是继承不了getBorder这个属性呀??????

unbreakable | 园豆:111 (初学一级) | 2012-04-09 10:39

@unbreakable: 

注意下面这两段代码的区别:

function All(border){
this.border=border;
this.getera=function (){
return -1;
}
}

function All2(border){
this.border=border;
}

All2.prototype.getera=function (){
return -1;
}


/*
三角形构造
*/
function Triangle(height,width){
All.call(this,3);
}

function Triangle2(height,width){
All2.call(this,3);
}

var t=new Triangle(20,4);
console.log(t.getera());//output -1

var t=new Triangle2(20,4);
console.log(t.getera()); //output t.getera is not a function
artwl | 园豆:16736 (专家六级) | 2012-04-09 10:47

有点明白了!!谢谢!!

unbreakable | 园豆:111 (初学一级) | 2012-04-09 11:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册