↑
Main Page
xbObjects
ClassZ.prototype.sayMessageZ = function () {
alert(this.messageZ);
};
ClassZ._initialized = true;
}
}
Note that two lines inherit the properties (using the
apply()
method), and two lines inherit the meth-
ods (using the
inheritFrom()
) method. As discussed earlier, the order in which the inheritance hap-
pens is important, and it is generally better to always inherit methods in the same order as the properties
(meaning that if properties are inherited from
ClassX
and then
ClassY
, the methods should be inher-
ited in that same order).
The following code tests the multiple inheritance example:
var objZ = new ClassZ();
objZ.sayMessageX(); //outputs “This is X message. “
objZ.sayMessageY(); //outputs “This is Y message.”
objZ.sayMessageZ(); //outputs “This is Z message.”
The previous code calls three methods:
1.
sayMessageX()
, which is inherited from
ClassX
, accesses the
messageX
property, also
inherited from
ClassX
.
2.
sayMessageY()
, which is inherited from
ClassY
, accesses the
messageY
property, also
inherited from
ClassY
.
3.
sayMessageZ()
, which is defined in
ClassZ
, accesses the
messageZ
property, also defined in
ClassZ
.
These three methods should output the appropriate message from the appropriate property, indicating
that the multiple inheritance has succeeded.
xbObjects
Netscape’s DevEdge site (
http://devedge.netscape.com
) contains a lot of useful information and
scripting tools for Web developers. One such tool is xbObjects (available for download from
http://
archive.bclary.com/xbProjects-docs/xbObject/
), written by Bob Clary of Netscape
Communications in 2001, when Netscape 6 (Mozilla 0.6) was first released. It supports all
versions of Mozilla since that time as well other modern browsers (IE, Opera, Safari).
Purpose
The purpose of xbObjects is to provide a stronger object-oriented paradigm to JavaScript, allowing not
only for inheritance but also for overloading of methods and the capability to call superclass methods.
To do this, xbObjects requires a number of steps be followed.
First, you must
register
the class, and in doing so, define which class to inherit from. This is done using
the following call:
_classes.registerClass(“Subclass_Name”, “Superclass_Name”);
120
Chapter 4
07_579088 ch04.qxd 3/28/05 11:36 AM Page 120
Free JavaScript Editor
Ajax Editor
©
→