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

 1 // JavaScript Document
 2 //创建三个构造函数
 3 function Shape(){
 4 this.name='ahape';
 5 this.toString=function(){return this.name;}
 6 }
 7 
 8 function TwoDShape(){
 9 this.name=''2D shape;
10 }    
11 function Triangle(side,height){
12 this.name='Triangel';
13 this.side=side;
14 this.height=height;
15 this.getArea=function(){return this.side * this.height/2;}
16 }    
17 
18 //实现继承
19 TwoDShape.prototype=new Shape();
20 Triangle.prototype=new TwoDShape();
21 //实现继承关系后,重置原型的构造器属性
22 TwoDShape.prototype.constructor=TwoDShape;
23 Triangle.prototype.constructor=Triangle;
24 //创建一个Triangle对象,并调用getArea方法
25 var my=new Triangle(5,10);
26 my.getArea();

 

posted @ 2016-11-23 15:00  浪涛飞  阅读(152)  评论(0编辑  收藏  举报