Overview
Destructuring assignment is an extension of the assignment operator.
It is a kind of pattern matching for arrays or objects, and then assigns values to the variables within them.
It is concise and readable in code writing, with clearer semantics; it also facilitates the retrieval of data fields in complex objects.
Destructuring model
In destructuring, the following two parts are involved:
- The target of destructuring, the left-hand side of the destructuring assignment expression.
Destructuring of array model (Array)
Basic
Nestable
Ignorable
Incomplete destructuring
Rest operator
Strings, etc.
In array destructuring, if the destructuring target is an iterable object, destructuring assignment can be performed. An iterable object is data that implements the Iterator interface.
Destructuring default values
When the destructuring pattern has a match result and the match result is undefined, the default value will be triggered as the returned result.
- The match result of a and b is undefined, triggering default values:a = 3; b = a =3
- a is normally destructured and assigned, match result: a = 1, b's match result is undefined, triggering default value:b = a =1
- a and b are normally destructured and assigned, match result:a = 1,b = 2
Destructuring of object model (Object)
Basic
Nestable and ignorable
Incomplete destructuring
Rest operator
Destructuring default values