Wednesday, September 23, 2015

on destructuring

[5:42 PM] Darien Valentine: [ x, y ] = z || []
[5:44 PM] Darien Valentine: and when destructuring in parameters, for the same reason sometimes you'll want to supply an empty default
[5:44 PM] Darien Valentine: function poop({ color, length, aroma }={}) {}
[5:46 PM] Darien Valentine: you can also supply defaults in destructured assignments that occur anywhere
[5:47 PM] Darien Valentine: const { poopCount=0 } = event;
[5:48 PM] Darien Valentine: the trickiest part of destructuring syntax at first is the distinction between the bindings you're creating & actual property names. effectively, in the examples above and most that you find (with objects), those bindings are actually a form of shorthand
they are doing double duty as both the binding (var, const, let) as well as the property name
the long form would look like this:
[5:49 PM] Darien Valentine: const { poopCount: poopCount=0 } = event;
meaning the binding identifier there -- the second poopCount -- could actually be another identifier; you're not stuck with the property name
[5:51 PM] Darien Valentine: last bit of info I can think of to confer:
you can destructure to things that already were declared, but if it's an object pattern, you need parens or another means to expressionize it (just like with object literals)
[5:53 PM] Darien Valentine: ({ cats } = obj);