浅谈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();

 

转载于:https://www.cnblogs.com/java-le/p/6093683.html

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-BlogPHP 1.7.3

Copyright www.122dh.com Rights Reserved.