- The Book of Dojo
- Quick Installation
- Hello World
- Debugging Tutorial
- Introduction
- Part 1: Life With Dojo
- Part 2: Dijit
- Part 3: JavaScript With Dojo and Dijit
- Part 4: Testing, Tuning and Debugging
- Part 5: DojoX
- The Dojo Book, 0.4
Mixins
Submitted by criecke on Sun, 04/29/2007 - 14:34.
Just as Dojo adds class-based inheritance to JavaScript, so it adds support for multiple inheritance. We do this through Dojo mixins. The methods and properties of a mixed-in class are simply added to each instance.
In pure object-oriented languages like Java, you must use typecasts to make an object "act like" its mixed-in class (in Java, this is through interfaces). Not in Dojo. You can use the mixed-in properties directly.
Suppose, for example, you have a class called VanillaSoftServe, and classes MandMs and CookieDough. Here's how to make a Blizzard:
dojo.declare("VanillaSoftServe",null, {
constructor: function() { console.debug ("mixing in Vanilla"); }
});
dojo.declare("MandMs",null, {
constructor: function() { console.debug("mixing in MandM's"); },
kind: "plain"
});
dojo.declare("CookieDough",null, {
chunkSize: "medium"
});
dojo.declare("Blizzard", [VanillaSoftServe, MandMs, CookieDough], {
constructor: function() {
console.debug("A blizzard with "+
this.kind+" M and Ms and "+
this.chunkSize+" chunks of cookie dough."
);
}
});
constructor: function() { console.debug ("mixing in Vanilla"); }
});
dojo.declare("MandMs",null, {
constructor: function() { console.debug("mixing in MandM's"); },
kind: "plain"
});
dojo.declare("CookieDough",null, {
chunkSize: "medium"
});
dojo.declare("Blizzard", [VanillaSoftServe, MandMs, CookieDough], {
constructor: function() {
console.debug("A blizzard with "+
this.kind+" M and Ms and "+
this.chunkSize+" chunks of cookie dough."
);
}
});
Then the following:
new Blizzard();
Will first print "mixing in Vanilla" on the debug console because VanillaSoftServe is the superclass of Blizzard. In fact, VanillaSoftServe is the only superclass of Blizzard - the first mixin is always the superclass. Next the constructors of the mixins are called, so "mixing in MandMs" will appear. Then "A blizzard with plain M and Ms and medium chunks of cookie dough." will appear.
Mixins are used a lot in defining Dijit classes, with most classes extending Dijit._Widget and mixing in Dijit._Templated.
- Printer-friendly version
- Login or register to post comments
- Unsubscribe post
Seems like a good place to describe dojo.mixin.
This page seems like a good place to explain the usage of dojo.mixin (pulled from the API)...
Many class constructors often take an object which specifies values to be configured on the object. In this case, it is often simplest to call dojo.mixin() on the 'this' object:
constructor: function(properties){
// property configuration:
dojo.mixin(this, properties);
console.debug(this.quip);
// ...
},
quip: "I wasn't born yesterday, you know - I've seen movies.",
// ...
});
// create an instance of the class and configure it
var b = new acme.Base({quip: "That's what it does!" });
this is also covered a
this is also covered a little more over at dojo campus:
http://dojocampus.org/content/?p=50
Hmmm
How does Dojo handle conflicts? The problem with multiple inheritance and the reason Java opted for interfaces is the inherent danger that superclasses share method signatures and variable identifiers, in which case a child of both will need to decide which to inherit. In the example above all you need to do is add a chunkSize variable to MandMs. When I type this.chunkSize in the Blizzard class what do I see? Is an error thrown ? If so when - compile time or runtime?