6

I have seen this in a piece of JS code:

var {status, headers, body} = res;

What does it do?

jd.
  • 4,057
  • 7
  • 37
  • 45
  • In some helma.org source code. – jd. Mar 04 '11 at 06:34
  • 2
    See [Destructuring assignment in JavaScript - Stack Overflow](http://stackoverflow.com/questions/204444/destructuring-assignment-in-javascript). Note that Javascript 1.7 (everything beyond 1.5, really) is effectively Mozilla-only. – ephemient Mar 04 '11 at 06:39
  • 1
    ephemient: Destructuring bind was my first thought, too, but I don't see any form that uses braces `{}` in the left-hand side. – Ken Mar 04 '11 at 06:41
  • To clarify what ephemient is saying: this will only work in Firefox. Chrome, Safari, & IE all don't support this. – Andrew Marshall Mar 04 '11 at 06:42
  • I tried this in Rhino (Javascript 1.7) and, unsurprisingly, it just generates a syntax error. I don't see anything in the Javascript 1.8 or 1.8.1 release notes that looks quite like this, either. – Ken Mar 04 '11 at 06:43

3 Answers3

1

i read something different from your expression here . this may help u

 var { a:x, b:y } = { a:7, b:8 };
 Print(x); // prints: 7
 Print(y); // prints: 8
xkeshav
  • 53,360
  • 44
  • 177
  • 245
1

nice method to set few variables at once from an object (open firebug and paste this to console)

var status=4;
var headers=4;
var body=4;

var res = {status:1, headers:2, body:3};
window.alert(status);
var {status, headers, body} = res;
window.alert(status);
fazo
  • 1,807
  • 12
  • 15
0

Looks like a destructuring attempt on a variable named res. I've never seen that in Javascript and Chrome console suggests that it's an error:

> var res = [ 1, 2, 3 ];
> var {status, headers, body} = res;
SyntaxError: Unexpected token {

Firebug console on Firefox 4b12 doesn't complain however but the statement seems to have no effect:

> var res = [ 1, 2, 3 ];
> var {status, headers, body} = res;
> status
undefined
> headers
undefined
> body
undefined
FilipK
  • 626
  • 1
  • 4
  • 13