Parsing assignment of ECMAScript6 entry variables

 

ES6 allows to extract values ​​from arrays and objects and assign values ​​to variables according to a certain pattern, which is called Destructuring

Explanation of this sentence: The first step extracts the value from the array or object, and the second step assigns the extracted value to the variable

 

1 Analytical assignment of an array

If the parsing is unsuccessful, the corresponding variable is undefined, and if the right side of the equal sign is not a traversable data structure, an error will also be reported.

Parsing allows the existence of default values. Note that ES6 internally uses the strict equality operator to determine whether a position has a value, so the default value will only work when a member of an array is strictly equal to undefined

   

2 Analysis and assignment of objects

The difference between object parsing and array parsing is that the array is in a certain order, and the value of the variable depends on its position; while the properties of the object have no order, the variable must be the same as the property name of the object, in order to correctly fetch to the value.

 

Similar to arrays:

  1 The destructuring assignment of objects can also be nested,

  2 In addition, the object can also define a default value, and the value of the property is undefined, the default value will have an effect

  3 If the parsing fails, the value is undefined.

Note: Pay attention to whether it is a pattern or a variable. The variables are separated by commas, and the pattern is followed by a colon.

3 Numeric, Boolean, String destructuring assignment

 

 

4 The role of deconstruction

  4.1: Swap values ​​of variables; return multiple values ​​from functions; assign values ​​to multiple parameters of functions; extract JSON values

to be continued...........

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342770&siteId=291194637