dojo.mixin
<script src="../../js/dojo.js"></script>
defined in dojo/_base/_loader/bootstrap.js
dojo.mixin
can mix multiple source objects into a
destionation object which is then returned. Unlike regular
for...in
iteration, dojo.mixin
is also smart about avoiding
extensions which other toolkits may unwisely add to the root
object prototype
parameter | type | description |
---|---|---|
obj | Object | If more than one of these objects contain the same value, the one specified last in the function call will "win". |
props | Object | Repeating. One or more objects whose values are successively copied into |
Examples
Example 1
make a shallow copy of an object
var copy = dojo.mixin({}, source);
Example 2
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:
dojo.declare("acme.Base", null, {
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!" });
Example 3
copy in properties from multiple objects
var flattened = dojo.mixin(
{
name: "Frylock",
braces: true,
}
{
name: "Carl Brutanananadilewski"
}
);
// will print "Carl Brutanananadilewski"
console.debug(flattened.name);
// will print "true"
console.debug(flattened.braces);