News & Info

CSA Daily Updates and Tech Chatter

Returning Multiple Values

Mozilla’s Javascript platform supports returning multiple values, as of version 1.7. I don’t know when that got incorporated into the XUL platform, but the features from 1.7 are usable there. In fact, we already use this in ColorFish:

this.getHSL = function () {
    var h, s, l;

    // ...

    return [h, s, l]
}

The last line returns an array with three values. This is how we pass back multiple values. If we are only interested in one return value then we can use this approach:

this.getHue = function(){
    return this.getHSL()[0];
}

this.getSaturation = function(){
    return this.getHSL()[1];
}

this.getLightness = function() {
    return this.getHSL()[2];
}

We can also get all of the values using destructuring binding. That is a fancy way of saying that when we assign a value, the left side of the assignment describes the shape of the value on the right side. For example:

var [h, s, l] = getHSL();
print(h);
print(s);
print(l);

On the first line, the left side matches the shape of the right side, the three-value array returned by getHSL(). If needed, we can ignore certain values.

var [h, , l] = getHSL();

// This is also legal if we don’t care about return values.  The
// difference between this and just calling getHSL() is that this
// makes it clear we are ignoring the return values.

var [,,] = getHSL();

This mechanism is intended to be the main way we pass back multiple-values in Javascript on the Mozilla platform.

But destructuring has other uses. The multiple assignments happen in parallel, not in sequence. So we can use destructuring to swap values without the need for temporary variables.

[a, b, c] = 

// Now a = c, b = a, and c = b

We can also use destructuring to get at objects.

var Lobby = { first: “Lobby”, last: “Jones” };

for (let [name, value] in Lobby) {
    // ...
}

Here we pull out each name and value of the properties as a pair. If we have a complex object, we can use an object on the left side of our bind to describe only the properties we want to extract into variables. Here is a good example from Mozilla’s site:

var people = [
  {
    name: "Mike Smith",
    family: {
      mother: "Jane Smith",
      father: "Harry Smith",
      sister: "Samantha Smith"
    },
    age: 35
  },
  {
    name: "Tom Jones",
    family: {
      mother: "Norah Jones",
      father: "Richard Jones",
      brother: "Howard Jones"
    },
    age: 25
  }
];

for each (let {name: n, family: { father: f } } in people) {
    print("Name: " + n + ", Father: " + f);
}

The key part here is the object sturcture to the left of ‘in’.

{
    name: n,
    family: {
        father: f
    }
}

This tells Javascript to pull out the name property and bind its value to ‘n’, and to take out the father property from inside the family object and bind that to ‘f’. This object is just a partial blueprint of the structure of ‘people’, naming the properties that we care about.

Tags: , , ,

About Lance Cleveland

I started my high-tech career in the early 80's as a computer technician. I became a lead engineer at a Boston area database company a few years later. When the Internet was just starting to show up on people's radar I quit my corner-office job and founded ProActive Web Marketing, my first start up company. That was the genesis of several successful start up companies including Time Magazine award winner The Lobster Net. After brief retirement in my mid-30s I co-founded the software consulting firm, Cyber Sprocket Labs. In addition to being "man of all hats" at Charleston Software Associates, I currently serve on the board or as technical adviser for several companies including Musiplicity, Model Locate, and Advanced Media Ltd. In the past I consulted for Data General, Kimberly Clark, Kraft, Philip Morris, Rich Foods, Telefonica, Aribtron, and a half-dozen other Fortune 500 companies. I've appeared as a keynote speaker for the USVI Economic Development Summit, showed up as a lead interviewee for Microsoft infomercials, and have been a cited performance advertising, Internet retail, and cybercrime expert in The Wall Street Journal and New York Times. I currently spend most of my time hanging with friends & family while hacking WordPress plugins. ### Code geek. Dad. Husband. Rum Lover. Not necessarily in that order.

Socialize

Enter your email below to sign up for the monthly Store Locator Plus newsletter. Click the Facebook icon to get almost-daily updates on what I'm working on now. The RSS feed icon will bring my bi-weekly blog posts to your feed reader.

Comments are closed.