Main Page

Polygons reloaded

In this case, the superclass
init()
method is being called first, which is required for xbObjects to work.
Even though
ClassA
didn’t register a superclass, a default superclass for all classes is created using
xbObjects, which is where this superclass
init()
method comes from.
The fourth and final step is to add the other class methods inside of the prototype function:
_classes.registerClass(“ClassA”);
function ClassA(sColor) {
_classes.defineClass(“ClassA”, prototypeFunction);
this.init(sColor);
function prototypeFunction() {
ClassA.prototype.init = function (sColor) {
this.parentMethod(“init”);
this.color = sColor;
};
ClassA.prototype.sayColor = function () {
alert(this.color);
};
}
}
Then, you can create an instance of
ClassA
in the normal way:
var objA = new ClassA(“red”);
objA.sayColor(); //outputs “red”
Polygons reloaded
At this point, surely you’re wondering if you will have a chance to see the polygon example redone
using xbObjects, so here it goes.
First, rewrite the
Polygon
class, which is very simple:
_classes.registerClass(“Polygon”);
function Polygon(sides) {
_classes.defineClass(“Polygon”, prototypeFunction);
this.init(sides);
function prototypeFunction() {
Polygon.prototype.init = function(iSides) {
this.parentMethod(“init”);
this.sides = iSides;
};
122
Chapter 4
07_579088 ch04.qxd 3/28/05 11:36 AM Page 122


JavaScript EditorFree JavaScript Editor     Ajax Editor


©