Dojo’s little gems

I really enjoyed Mitch’s article on YUI Lang’s little gems and thought it might be handy to see how to do the same things with Dojo. Here’s the translation:

Trim

var trimMe = "  trim me please  ";

//YUI 
var trimmed = YAHOO.lang.trim(trimMe);

//Dojo
// - exactly the same
trimmed = dojo.trim(trimMe);

Substitute

var processText = function(key, value) {  
  return value.toUpperCase();  
};  

//YUI
YAHOO.lang.substitute('Hello {world}', { world: 'earth'}, processText); 

// Dojo
// - Named placeholders are prefaced with a $
// - No support for extraValues from Mitch's example
dojo.require("dojo.string"); // not in base distribution
dojo.string.substitute('Hello ${world}', { world: "earth" }, processText);

Later

// YUI 
// - Docs indicate this return type void, which is a timer object?
YAHOO.lang.later(200, foo, "method", [{ data: "bar" }]);

// Dojo
// - No abstraction for timers in Dojo
// - Can use hitch to set calling scope for function
setTimeout(dojo.hitch(foo, "method", { data: "bar" }), 200);
//  or
setInterval(dojo.hitch(foo, "method", { data: "bar" }), 200);

Merge

var defaults = { color: "red", answer: 42 };
var config;

//YUI
config = YAHOO.lang.merge(defaults, { color: "blue" });

//Dojo
// - mixin takes a variable number of arguments
// - Values are written to the first object, hence passing {}
// - Later values have precendence
config = dojo.mixin({}, defaults, { color: "yellow" });

One response

  1. An improvement for the last one …

    config = dojo.delegate(defaults, {color:”yellow”});

    Rob :)

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.