Deconstruction assignment

An array of deconstruction

[a,b] = [1,2]. If the left array contains multiple variables c, c will be assigned undefined if c fails to be deconstructed

Parameter Default Value

  • [a= 10,b =5] = [2,3]. If no value is passed on the right side, the default value will be used and the default value will be overwritten
  • The default value on the left is used only if the value on the right is exactly (===) undefined. ‘undefined’ and null override the default value
  • If the default value is an expression, the expression is executed only if the default value is used (lazy evaluation)
  • The default value can refer to another variable in a deconstructed assignment, but that variable must be a declared variable
  • Often used as a parameter to a function

Object to deconstruct

The characteristics of

  • Same structure on both sides

  • Object attribute disorder

  • The variable name must equal the attribute name

    • If deconstruction fails, the variable name is assigned undefined

The internal mechanism

  • The internal mechanism for deconstructing assignment of an object is to find the property of the same name and then assign it to the corresponding variable. It is the latter, not the former, that is really assigned.
  • let { foo: foo, bar: bar } = { foo: ‘aaa’, bar: ‘bbb’ }; Where the first foo is the matched pattern and the second foo is the assigned variable
  • Object destructuring assignment is a shorthand for the above form
  • This mechanism allows you to assign object attributes to variables with different names
  • The deconstructive assignment of an object can take inherited properties, properties of its archetype

The default value

  • As with array deconstruction, the default value is valid only if the object’s property value is strictly equal to undefined

Nested structure

  • Since there is no one-to-one mapping of arrays, there can be multiple variables that get children of an object at different levels of nesting
  • An error is reported if the deconstruction pattern is a nested object and the parent property of the child object does not exist.

application

  • A method that extracts properties of an existing object

Pay attention to

1. To perform deconstruction assignment of declared variables, the entire deconstruction assignment expression should be enclosed by () to avoid the deconstruction object being parsed as a code block at the beginning of the line

2. The pattern to the left of the deconstruction assignment can be left with no variables, no effect, but no error

Let {0: first, [arr. Length-1] : last} = arr; Gets the first and last number of the array

If one object is equal to one object, it means that the latter object is the default value of the previous object. If the external argument object is not undefined, it will be assigned to the former object instead of the default value. Then the two objects are deconstructed, which is equal to two deconstructed assignments

5. Destruct assignment of other data types

  • Strings can also be destructed and assigned, in which case the string is converted to an array-like object, and even its length property can be obtained
  • Values and Boilervalues can also be deconstructed and converted to their wrapper objects, which can obtain properties they have, such as toString
  • Undefined and NULL cannot be converted to objects, so destructuring them will report an error

It is recommended that parentheses () not be used in schemas (as mentioned in object destructuring internals)

  • The variable declaration statement is not available
  • Function arguments are not available
  • The pattern part of an assignment statement
  • Only the non-schema part of the assignment statement can be used

application

Swap variable values such as [a,b] = [b,a]

Values from arrays or variables

Function parameters pass values and set default values

Extracting JSON data

The foundation determines the future, one step at a time