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

Inheritance example code in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:07:281054browse
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());
Statement:
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