Please note this post has not been updated and may contain broken links and outdated information.

I was pretty new to javascript when I started using the YUI. For a long time, YAHOO was just that file I had to make sure to include. It was a few months after using the YUI (and was more comfortable with js in general) that I happened to peek at the YAHOO.lang API. It doesn’t get much attention, but it’s got a few useful functions you may not realize are along for the ride (since about version 2.3) – functionality you may be duplicating.


If this is really the first time you’re looking at YAHOO.lang, one of the most basic uses of it is type checking. Functions like isNull(), isUndefined(), isValue(). (type ‘is’ into the search boxto see all the types you can check for).

trim() and a shave

Then there’s trim(). Really, who doesn’t need trim? Strips whitespace around a string. If what you pass in isn’t a string, returns the input as is.

accept no substitute()-tions

Some of you may be familiar with sprintf() from PHP land (or elsewhere). YAHOO.lang has it’s own sprintf – substitute(). Similar to sprintf, except you
use {key : value}. Basic usage: (You can copy these examples into Firebug and they will work)

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

returns: Hello earth You can also process the text before substitution:

var processText = function(key, value) {
  return value.toUpperCase();
YAHOO.lang.substitute('Hello {world}', {'world':'earth'}, processText);

returns Hello EARTH And even pass in extra info:

var processText = function(key, value, extraInfo) {
  if(!YAHOO.lang.isNull(extraInfo)) {
    return extraInfo;
  return value.toUpperCase();
YAHOO.lang.substitute('Hello {world Venus, Jupiter}', {'world':'earth'}, processText);

returns Hello Venus, Jupiter

see you later()

Next up: later(). A wrapper function for setTimeout() and setInterval(). By default, it acts like setTimeout(), but you can set periodic to true to make setInterval.
Basic Usage (to setInterval)

var foo = {
  count :0,
  'method' : function(data) {
    if(this.count == 10) {
var timer = YAHOO.lang.later(1000, foo, 'method', [{data:'bar', data2:'zeta'}], true);`

merge() ahead

Last (certainly not least) comes merge(). This will allow you to merge properties from 2 or more objects. Very handy for making a config object, joining defaults with user supplied. To whit:

var myAwesomelWidget = function(oConfigs) {
  oConfigs = oConfigs || {};
  var defaults = {
    'awesomeness' : '11',
    'shiny'       : 'high',
    'sparkle'     : 'high'

  var combinedConfigs = YAHOO.lang.merge(defaults, oConfigs);


//Shiny is now set to low, everything else in combinedConfigs is set to the defaults
myAwesomelWidget({'shiny': 'low'});`


There’s more in there, and I encourage you to have a look.

Vous en voulez plus? Rejoignez notre infolettre.

Nous aimons partagé nos conseils à propos du web, nos idées et nos projets.


Vous pouvez vous désinscrire en tout temps. Contactez-nous pour plus de détails.