浅谈javaScript中的继承关系<一>

时间:2024-07-26 12:36:26
 // JavaScript Document
//创建三个构造函数
function Shape(){
this.name='ahape';
this.toString=function(){return this.name;}
} function TwoDShape(){
this.name=''2D shape;
}
function Triangle(side,height){
this.name='Triangel';
this.side=side;
this.height=height;
this.getArea=function(){return this.side * this.height/2;}
} //实现继承
TwoDShape.prototype=new Shape();
Triangle.prototype=new TwoDShape();
//实现继承关系后,重置原型的构造器属性
TwoDShape.prototype.constructor=TwoDShape;
Triangle.prototype.constructor=Triangle;
//创建一个Triangle对象,并调用getArea方法
var my=new Triangle(5,10);
my.getArea();