topical media & game development
professional-javascript-04-zInheritExample1.htm / htm
<html>
<head>
<title>Example</title>
<script type="text/javascript" src="zinherit.js"></script>
</head>
<body>
<script type="text/javascript">
function Polygon(iSides) {
this.sides = iSides;
}
Polygon.prototype.getArea = function () {
return 0;
};
function Triangle(iBase, iHeight) {
Polygon.call(this, 3);
this.base = iBase;
this.height = iHeight;
}
Triangle.prototype.inheritFrom(Polygon);
Triangle.prototype.getArea = function () {
return 0.5 * this.base * this.height;
};
function Rectangle(iLength, iWidth) {
Polygon.call(this, 4);
this.length = iLength;
this.width = iWidth;
}
Rectangle.prototype.inheritFrom(Polygon);
Rectangle.prototype.getArea = function () {
return this.length * this.width;
};
var triangle = new Triangle(12, 4);
var rectangle = new Rectangle(22, 10);
alert(triangle.sides);
alert(triangle.getArea());
alert(rectangle.sides);
alert(rectangle.getArea());
alert(triangle.instanceOf(Triangle));
alert(triangle.instanceOf(Polygon));
alert(rectangle.instanceOf(Rectangle));
alert(rectangle.instanceOf(Polygon));
</script>
</body>
</html>
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.