JavaScript is an object-oriented programming language, but unlike the class-based languages of Java or C++, it uses prototypal inheritance as its mechanism of choice instead of a class-based paradigm. Consequently, mixing properties into object instances as part of a "has-a" relationship is often a far more natural pattern than attempting to mimic class-based patterns that espouse an "is-a" relationship. Consider the following example that adds in a collection of properties to an object instance all at once, using dojo. mixin:

/. obj gets passed around and lots of interesting things happen to it "* /
1/ now, we need to add in a batch of properties ... doja .mlxin (obj I {
The dojo. extend function works much like dojo. mixin except that it manipulates a constructor function's prototype instead of the specific object instances.
Of course, there are some design patterns that do lend themselves to inheritance hierarchies, and the dojo.declare function is your ticket to mimicking class-based inheritance if you find yourself in a predicament that calls for it.

Name:  Pub sub communication.jpg
Views: 188
Size:  96.2 KB

You pass it the fully qualified name of the "class" you'd like to create, any ancestors that it should inherit from, and a hash of any additional properties. The dojo. declare function provides a built-in construction function that gets run, so any parameters that are passed in can be handled as needed. Here's a short example demonstrating a Baz class that multiplies inherits from both a Foo and a Bar class:
I/create an Ij.Foo that doesn't have any ancestors dojo.declare("lj.Foo", null,
_name : null, constructor : function (name) this._name = name;
I.
talk function() {alert("I'm "tthis._name); I,
customFooMethod function () ( / '/ )
/ /create an lj. Bar that doesn't have any ancestors dojo.declare("lj.Bar", null,
_name: null,
constructor: functio!'.(name)
talk function () {alert ("I'm "+this. name); l.
customBarMethod function () { /. ... '/ I
//create an Ij.Baz that multiply inherits dojo.declare("lj.Baz", [lj.Foo, Ij.Barl.
1* custom properties go here" I _name : null,
constructor: function (name)
/Iparameters get passed into z.he special constructor f'Jnction bartyBaz :0 new Ij. Baz ("barty") ;

When each of the dojo. declare statements is encountered, internal processing leaves a function in memory that can be readily used to instantiate a specific object instance with the new operator-just like plain- old JavaScript works. ln fact, the bartyBaz object is one such instantiation. It inherits the customFooMethod and customBarMethod from ancestors, but provides its own talk method. In the event that it had not provided its own talk method, the last one that was mixed in from the ancestors would have prevailed. In this particular case, the ancestors were [lfFoo, lfBar], so the last mixed in ancestor would have been lfBar. If defined, all classes created with