What is deconstruction assignment

ES6 allows you to extract values from arrays and objects and assign values to variables in a pattern known as deconstruction.

  • Function: Assigns a value to a variable
  • The value of a variable comes from an array or object
  • Rules: There are certain patterns

2. Array deconstruction assignment

Assign values one by one with subscripts:

3. Object deconstruction assignment

Deconstruction rules:

It has to be associated by a key name. If there is no key name, it is undefined. The internal mechanism of object deconstruction assignment is to find the property of the same name and then assign it to the corresponding variable. The order of the key names does not matter.

Assign an attribute of an existing object to a variable

4. The application of deconstruction assignment

1. Swap variable values

Conventional methods

2. Quickly get multiple values returned from the function

3. Quickly obtain object attribute values