title: Destructuring assignment in JavaScript
body: <p>As can be seen in the Mozilla changlog for JavaScript 1.7 they have added destructuring assignment. Sadly I'm not very fond of the syntax (why write a and b twice?):</p>
  
  <pre><code>var a, b;  
  [a, b] = f();
  </code></pre>
  
  <p>Something like this would have been a lot better:</p>
  
  <pre><code>var [a, b] = f();
  </code></pre>
  
  <p>That would still be backwards compatible. Python-like destructuring would not be backwards compatible.</p>
  
  <p>Anyway the best solution for JavaScript 1.5 that I have been able to come up with is:</p>
  
  <pre><code>function assign(array, map) {
      var o = Object();
      var i = 0;
      $.each(map, function(e, _) {
          o[e] = array[i++];
      });
      return o;
  }
  </code></pre>
  
  <p>Which works like:</p>
  
  <pre><code>var array = [1,2];
  var _ = assign[array, { var1: null, var2: null });
  _.var1; // prints 1
  _.var2; // prints 2
  </code></pre>
  
  <p>But this really sucks because _ has no meaning. It's just an empty shell to store the names. But sadly it's needed because JavaScript doesn't have pointers. On the plus side you can assign default values in the case the values are not matched. Also note that this solution doesn't try to slice the array. So you can't do something like <code>{first: 0, rest: 0}</code>. But that could easily be done, if one wanted that behavior.</p>
  
  <p>What is a better solution?</p>