What is var {u, v, w} = x; mean in javascript?

I saw this in a piece of JS code:

var {status, headers, body} = res; 

What is he doing?

+6
javascript
source share
3 answers

a good way to set several variables from an object at once (open firebug and paste it into the 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); 
+1
source share

I am reading something different from your expression here . it may help u

  var { a:x, b:y } = { a:7, b:8 }; Print(x); // prints: 7 Print(y); // prints: 8 
+1
source share

It looks like an attempt to destructure a variable called res . I have never seen this in Javascript and the Chrome console, that this is an error:

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

The Firebug console on Firefox 4b12 does not complain, but the statement does not seem to have an effect:

 > var res = [ 1, 2, 3 ]; > var {status, headers, body} = res; > status undefined > headers undefined > body undefined 
0
source share

All Articles