Datasheet
Chapter 1: Enhancing Development with Dojo Core
Now, consider the following code to see how you can override methods inherited from parent
classes:
dojo.declare("decafbad.school.MaleStudent", decafbad.school.Student, {
getName: function() {
var name = this.inherited(arguments);
return "Mr. " + name;
}
});
This
MaleStudent
class inherits the
getName()
method from
Student
, but overrides it. The new imple-
mentation uses
this.inherited(arguments)
provided by Dojo to call the parent class method and put
its own spin on the return value. Other than the special case of
constructor
, no overridden parent
methods are automatically called in subclasses.
Note that the
arguments
parameter to
this.inherited()
is a built-in JavaScript feature. This call
convention allows you to easily pass along all of the arguments originally given to the current
method.
Using Multiple Inheritance through Mixins
Dojo also supports multiple inheritance in the form of mixins. Take a look at this new example:
dojo.declare("decafbad.school.DoorUnlocker", null, {
canUnlockDoors: true,
constructor: function() {
this.doorsUnlocked = [];
},
unlockDoor: function(door) {
this.doorsUnlocked.push(door);
return door + " now unlocked";
}
});
dojo.declare("decafbad.school.DormAssistant",
[ decafbad.school.Student, decafbad.school.DoorUnlocker ], {});
Two classes are defined here, named
DoorUnlocker
and
DormAssistant
.
The first class,
DoorUnlocker
, does not inherit from any parent classes but defines a property
canUnlockDoors
, a constructor, and a method
unlockDoor
.
The second class,
DormAssistant
, uses an array literal to declare inheritance from both
Student
and
DoorUnlocker
. These are called mixin classes. This means that Dojo mixes them in — it adds all of the
properties and methods from each mixin into the
DormAssistant
class, in the order that they appear in
the inheritance list. The exception to this is constructors: They’re accumulated in an internal list for the
new class and each is called in order whenever a new instance is created.
Thus,inthisexample,
DormAssistant
is a
Student
given the additional add-on capability to perform
unlockDoor()
.The
Student
class, first in the inheritance list, is the official parent class of
DormAssistant
.
The
DoorUnlocker
class is treated as extra capabilities sprinkled in.
13