↑
Main Page
prototypeFunction
Here, the subclass and superclass names are passed in as strings, not as pointers to their constructors.
This call must come before the constructor for the given subclass.
You can also call
registerClass()
with only the first argument if the new class doesn’t inherit from
another class.
The second step is to call the
defineClass()
method inside of the constructor, passing in the name of
the class as well as a pointer to what Clary calls a
prototype function
, which is used to initialize all proper-
ties and methods for the object (more on that later). For example:
_classes.registerClass(“ClassA”);
function ClassA(color) {
_classes.defineClass(“ClassA”, prototypeFunction);
function prototypeFunction() {
//...
}
}
As you can see, the prototype function (aptly named
prototypeFunction()
) is located inside of the
constructor. Its main purpose is to assign all methods to the class when appropriate (it works like
dynamic prototyping in this way).
The next step (that’s three so far) is to create an
init()
method for the class. This method is responsible
for setting up all properties for the class and must accept the same arguments as the constructor itself.
By convention, the
init()
method is always called after the
defineClass()
method is called. For
example:
_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;
};
}
}
You may have noticed a method named
parentMethod()
being called in the
init()
method. This is
the way that xbObjects allows a class to call a superclass method. The
parentMethod()
accepts any
number of arguments, but the first argument is always the name of the parent class method to call (this
argument must be a string, not a function pointer); all other arguments are passed to the superclass
method.
121
Inheritance
07_579088 ch04.qxd 3/28/05 11:36 AM Page 121
Free JavaScript Editor
Ajax Editor
©
→