Main Page

registerClass

Polygon.prototype.getArea = function () {
return 0;
};
}
}
Next, rewrite the
Triangle
class, which is the first taste of real inheritance in this example:
_classes.registerClass(“Triangle”, “Polygon”);
function Triangle(iBase, iHeight) {
_classes.defineClass(“Triangle”, prototypeFunction);
this.init(iBase,iHeight);
function prototypeFunction() {
Triangle.prototype.init = function(iBase, iHeight) {
this.parentMethod(“init”, 3);
this.base = iBase;
this.height = iHeight;
};
Triangle.prototype.getArea = function () {
return 0.5 * this.base * this.height;
};
}
}
Note the
registerClass()
call just before the constructor, where the inheritance relationship is set up.
Also, the first line of the
init()
method calls the superclass (
Polygon
)
init()
with an argument of
3
,
which sets the
sides
property to
3
. Other than that, the
init()
method is very similar: a simple con-
structor, assigning the
base
and
height
.
The
Rectangle
class ends up looking very similar to
Triangle
:
_classes.registerClass(“Rectangle”, “Polygon”);
function Rectangle(iLength, iWidth) {
_classes.defineClass(“Rectangle”, prototypeFunction);
this.init(iLength, iWidth);
function prototypeFunction() {
Rectangle.prototype.init = function(iLength, iWidth) {
this.parentMethod(“init”, 4);
this.length = iLength;
this.width = iWidth;
}
123
Inheritance
07_579088 ch04.qxd 3/28/05 11:36 AM Page 123


JavaScript EditorFree JavaScript Editor     Ajax Editor


©