Destructuring assignment in JavaScript

First off, var [a, b] = f() works just fine in JavaScript 1.7 – try it!

Second, you can smooth out the usage syntax slightly using with():

var array = [1,2];
with (assign(array, { var1: null, var2: null }))
{
   var1; // == 1
   var2; // == 2
}

Of course, this won’t allow you to modify the values of existing variables, so IMHO it’s a whole lot less useful than the JavaScript 1.7 feature. In code I’m writing now, I just return objects directly and reference their members – I’ll wait for the 1.7 features to become more widely available.

Leave a Comment