Encapsulation

You are now equipped with a working knowledge of how you can use dojo.declare to define a class and employ Dojo’s simulated class-based inheritance mechanism, and you’ve already witnessed Dojo’s basic approach to encapsulation firsthand. For defining simple classes, you may have felt that Dojo does not provide a lot of additional value because you could just as easily achieve the same result without Dojo. However, some of encapsulation’s beauty became more apparent when we defined inheritance relationships; the inheritance relationship between a class and its ancestors was hopefully very obvious, because they were consecutive parameters to dojo.declare. Although you might be wondering why that’s such a big deal, it turns out that a class and its ancestors are traditionally much more spread out in “roll your own” JavaScript code, which makes that relationship not so obvious. Remember: readable code is more maintainable code. As we delve into the world of UI widgets, the benefits of encapsulation continue to become more and more apparent.

Encapsulation is a huge reason to be excited about OOP with Dojo, because it allows us to take the otherwise disjoint entities of JavaScript, CSS, and HTML, and wrap them all up into a nice cohesive widget that stands alone as a single logical entity. In addition to helping with the initially awkward task of wrapping your mind around a Dojo widget, encapsulation facilitates the entire gamut of maintenance that is unavoidable with any ...

Get Dojo Fundamentals: Creating Object-Oriented Widgets 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.