Object destructuring
You can also destructure objects:
1 2 3 4 5 6 | let o = { a: "foo" , b: 12, c: "bar" } let {a, b} = o; |
This creates new variables a
and b
from o.a
and o.b
. Notice that you can skip c
if you don’t need it.
Like array destructuring, you can have assignment without declaration:
1 | ({a, b} = {a: "baz" , b: 101}); |
Notice that we had to surround this statement with parentheses. JavaScript normally parses a {
as the start of block.
Please login to continue.