Extending Objects in Dojo

Dojo works a bit differently than we saw with jQuery and ExtJS. They have the notion of extend as well as mixin. These both behave differently, not only from each other, but from the previous jQuery.extend and Ext.apply. These methods in Dojo work right to left, different from our previous examples.

Let’s discuss mixin, this is more like what we previously saw with previous libraries’ methods:

var start = {
    id: 123,
    count: 41,
    desc: 'this is information',
    title: 'Base Object',
    tag: 'uncategorized',
    values: [1,1,2,3,5,8,13]
};
var more = {
    name: 'Los Techies',
    tag: 'javascript'
};
var extra = {
    count: 42,
    title: null,
    desc: undefined,
    values: [1,3,6,10],
    dojo: true
};

var extended = dojo.mixin(extra, more, start);
console.log(JSON.stringify(extended));â

And the output:

{
    "count": 41,
    "title": "Base Object",
    "desc": "this is information",
    "values": [1,1,2,3,5,8,13],
    "dojo": true,
    "name": "Los Techies",
    "tag": "uncategorized",
    "id": 123
}

If you remember from the previous posts, we extended our start, more, and extra objects from left to right. Using a Dojo mixin, our arguments are passed to the function in the reverse order; this is by design. Everything in our output of the extended object remains as it was in our start object. The only difference is that more added the name property and extra added the dojo property. This works just like you’d expect a mixin function to work and just like our previous extend and apply from jQuery and Ext JS.

The dojo.extend function works on an object’s prototype and doesn’t follow the same patterns of this series of blog posts. For that reason, I’m going to skip it and let you do your own homework if you’re interested. ;)

For more information: dojo.mixin (Yes I know this way to reference dojo is deprecated, but the old version worked better in my tiny example.)

Related:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Chris Missal

Oh hey, I'm a Senior Consultant for Headspring in Austin, TX. I've been working in software professionally since 2006 and I really, really love it. I'm mostly in the Microsoft world, but enjoy building computer things of all sorts (to be vague). When I'm not slinging code, I'm probably out and about slinging discs, bowling balls, or good beer with great friends.
This entry was posted in JavaScript and tagged , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post.