Created
February 26, 2011 00:45
-
-
Save zpao/844787 to your computer and use it in GitHub Desktop.
Multiple (destructuring) assignment in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = ["yea, yea", "we knew this worked"]; | |
var [a, b] = arr; | |
// a == "yea, yea" | |
// b == "we knew this worked" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj = { | |
foo: "WTF", | |
bar: "this works?", | |
baz: "looks like it" | |
}; | |
var { foo: c, bar: d} = obj; | |
// c == "WTF" | |
// d == "this works?" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj2 = { | |
foo: { dude: "WTF" }, | |
bar: "this works too?", | |
baz: "looks like it" | |
}; | |
var { foo: e, bar: f} = obj2; | |
// e == { dude: "WTF" } | |
// d == "this works too?" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using the unchanged obj2 from above... | |
var { foo: { dude: g } } = obj2; | |
// g == "WTF" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment