<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这个属性呀??????
/*
四边形构造
*/
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;
}
我想问的是为什么可以继承getera这个属性,但是继承不了getBorder这个属性呀??????
@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
有点明白了!!谢谢!!