Datasheet

Chapter 1: Enhancing Development with Dojo Core
getName: function() {
return this.name;
}
};
Now, consider this functionally similar code from a module defined in
decafbad/school.js
:
dojo.provide("decafbad.school")
dojo.declare("decafbad.school.Person", null, {
constructor: function(name) {
this.name = name;
},
getName: function() {
return this.name;
}
});
The
dojo.declare()
method does all the behind-the-scenes work necessary to create a JavaScript class
with inheritable constructors and methods. The arguments to the method are the following:
Name of the class, complete with namespace.
Parent class from which to inherit, if any. In this case, it’s null, which indicates there are no par-
ent classes for this class.
Object literal defining the class, including all of its data members and methods.
Among the methods defined in a Dojo class,
constructor
is called whenever a new instance of the class
is created. This plays the same roll as the initial function definition in native JS prototype-based classes.
Declaring Subclasses and Overriding Methods
Considering further what Dojo does beyond standard JavaScript prototypes, take a look at this declara-
tion of a
Person
subclass:
dojo.declare("decafbad.school.Student", decafbad.school.Person, {
constructor: function(name, grade) {
// Note that the inherited constructor is automatically called.
this.grade = grade;
},
getGrade: function() {
return this.grade;
}
});
The new
Student
class is a subclass of
Person
— thanks to Dojo,
Student
inherits all the methods
of
Person
, including the constructor. When a new instance of
Student
is created, the
Person
class
constructor
method is automatically called before
constructor
is called for
Student
.
This is sort of class-based programming supported by other languages though it requires a little extra
work within JavaScript’s prototype-based environment to support inheritance and other typical OOP
facilities. This is what
dojo.declare()
provides.
12