Array Destructuring Memo | JavaScript: The Recent Parts

ref:
Kyle Simpson's JavaScript:The Recent Parts on Frontend Master

Destructuring is about assignment instead of declaration.

Normally we could have a better declarative presentation of the data pattern that we expect to be passed in by using Destructuring.

Destructuring syntax is esssentially sugar for refine imperative things we did before, any error happened before also happens in destructuring unless very few diverision.

Here we have 2 ways to compare non-destructuring assignment solution with destructuring pattern solution, and learn how destructuring can make it better declarative be.

1) element is missing? both will return undefined

lost 1 array element.png

2)element is over-provided? both will ignore

redundent 1 array element.png

3)element missing in the middle? both will reutrn undefined

undefined element withnin the array.png

4)!== undefined? both will replace with default value

default value.png

5)gather all elements left behind

collect all elements.png

6)set a relay variable also is fine

get access to value and destructive parts.png

7)feel free to declare firstly

daclaration and assignment.png

8)as long as it is a valid position, anything could be at the left side on the =

valid leftside target.png

9)destructuring pattern always point to the entire array

point to entire array.png

10)using comma ,to skip

array elision.png

11)declarative to swap values by destructuring

swap 2 values.png

12)put destrucuring pattern at parameter position

destructuring array parameter.png

13)fixing unexpected data value with setting default values

fallback default.png

14)also works in parameter position

parameter default value.png

15)also works on single element

default value for single element.png

16)nested array has nested des-pattern

nested array destructuring.png

17)default values also fix unexpected data value in nested array

default values in nested array.png

*setting default values is a good habit, remember to do it in advance.

评论

暂无评论