JavaScript Object Utilities
Three of Base's language utilities facilitate operations you may
need to routinely perform on objects: mixin, extend, and clone.
Tip
Dojo uses mixin and
extend in its dojo.declare implementation, which is the
toolkit's mechanism for simulating class-based inheritance. dojo.declare is covered extensively in
Chapter 10.
Mixins
JavaScript allows you to approximate lightweight classes by
housing a collection of properties and methods inside a constructor
function. You may then create object instances of these classes by
invoking the constructor function with the new operator. As you might expect, it can
sometimes be quite convenient to add additional properties to an
object, whether it be to make something dynamic happen on-the-fly or
as part of a well crafted design that maximizes code reuse. Either
way, mixin provides a compact way
of handling the implementation details for you.
Tip
In terms of object-oriented design with JavaScript, you'll
see mixin used extensively
throughout the toolkit to reuse blocks of code.
The API for using the toolkit's mixin functions entails providing an
indeterminate number of objects, where the first objects gets the
other objects mixed into it:
dojo.mixin(/*Object*/ o, /*Object*/ o, ...) //Returns Object
Here's an example of mixin:
function Man( ) { this.x = 10; } function Myth( ) { this.y = 20; } function Legend( ) { this.z = 30; } var theMan = new Man; var theMyth = new Myth; var theLegend = new Legend; function ManMythLegend( ) {} ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access