Chapter 10. Simulated Classes and Inheritance

Even though JavaScript loosely approximates classes with Function objects and offers a prototype-based inheritance mechanism that's a little different from what you may be used to if your background is a language like Java or C++, Dojo does a fine job of building upon native JavaScript constructs to simulate classes and class-based inheritance. This chapter dives into dojo.declare, the toolkit's vehicle for wiring up classes and inheritance and, in doing so, paves the way for a solid understanding of the Dijit infrastructure, which is coming up in Part II.

JavaScript Is Not Java

Before we get into the core discussion of simulating inheritance hierarchies and classes with Dojo, you must first grok that JavaScript is not Java, nor does Dojo try to fight against JavaScript's style and reimplement portions of the JavaScript language under the hood and force a square peg in a round hole—and this is a very good thing! JavaScript is an incredibly dynamic, weakly typed language, while Java is a more strongly typed language with real classes and class-based inheritance hierarchies that are defined at compile time. JavaScript has prototypal inheritance that can be used to simulate classes and is purely interpreted.

By embracing JavaScript for what it is and leveraging its language features in the most complementary way possible, Dojo benefits from enhancements to the language as it evolves, avoids the maintenance that comes hand-in-hand with massive layers of boilerplate. In the end, this provides you with a streamlined, agile implementation that can keep up with the "release early, release often" philosophy of the modern technological era.

With the notable exception of the form dijits that are introduced in Chapter 13, you won't see a lot of object-oriented design in Dojo because it's not the Dojo way. The Dojo way is to embrace prototypal inheritance and Base constructs like mixin and extend that take advantage of JavaScript's strengths. At the same time, Dojo does try to be pragmatic, and some topics do lend themselves to object-oriented design and are quite awkward to model without it. Actually, the very reason that this chapter is presented as the last of Part I is because Part II introduces Dijit, which is a topic that lends itself quite well to a class-based design.

As you read this chapter, keep this preamble in the back of your mind, because if you come from a strong background in a class-based object-oriented programming language like Java or C++, the temptation will be to superimpose that paradigm onto Dojo and to try to turn everything into an inheritance relationship, which isn't a very natural thing to do for reasons of style or performance. While this chapter does demonstrate the ability to use Dojo for the purpose of simulating deep hierarchies of objects, the assumption is that you'll use discretion when doing so.

Get Dojo: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.