Home > Web Front-end > JS Tutorial > Inheritance example code in javascript_javascript skills

Inheritance example code in javascript_javascript skills

WBOY
Release: 2016-05-16 18:07:28
Original
1043 people have browsed it
Copy code The code is as follows:

function Polygon(iSliders){ //Define a polygon
 this .silders=iSliders;
}
Polygon.prototype.getArea=function(){ //A method to define an area for a polygon
Return 0;
}
function Triangle(iBase ,iHeight){
Polygon.call(this,3); //Inherit the polygon object
this.base=iBase;
this.height=iHeight;
}
Triangle.prototype. getArea=function(){ //Rewrite the area method
Return 0.5*this.base*this.height;
}
var triangle=new Triangle(15,8); //Example Create a triangle
alert("number of sides:" triangle.silders);
alert("area:" triangle.getArea());
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template