关于javascript:Array-Destructuring-Memo-JavaScript-The-Recent-Parts

46次阅读

共计 1247 个字符,预计需要花费 4 分钟才能阅读完成。

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

2)element is over-provided? both will ignore

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

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

5)gather all elements left behind

6)set a relay variable also is fine

7)feel free to declare firstly

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

9)destructuring pattern always point to the entire array

10)using comma ,to skip

11)declarative to swap values by destructuring

12)put destrucuring pattern at parameter position

13)fixing unexpected data value with setting default values

14)also works in parameter position

15)also works on single element

16)nested array has nested des-pattern

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

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

正文完
 0