### Destruct assignment of arrays

  • It can be assigned as follows
    let [a, b, c] = [1.2.3];
    let [ , , third] = ["foo"."bar"."baz"];
    third // "baz"
    
    Copy the code

    If both patterns are the same, the variable will be assigned the corresponding value

  • Incomplete deconstruction
    let [x, y] = [1.2.3];
    x / / 1
    y / / 2
    Copy the code
  • If the data structure on the right has an Iterator interface, you can perform array destructuring assignments (such as generator functions).
  • The default value is allowed. The default value takes effect only when the value on the right of the equals sign is exactly equal to undefined
    let [x = 1] = [undefined];
    x / / 1
    let [x = 1] = [null];
    x // null
    Copy the code
  • If the default value is an expression, then the expression is lazily evaluated until it is used

Destruct assignment of the ### object

  • Object attributes have no order and can be assigned as long as the names are equal
    let { bar, foo } = { foo: 'aaa'.bar: 'bbb' };
    foo // "aaa"
    bar // "bbb"
    let { baz } = { foo: 'aaa'.bar: 'bbb' };
    baz // undefined
    Copy the code
  • You can also perform the following operations
    / / a
    let { log, sin, cos } = Math;
    / / two cases
    const { log } = console;
    log('hello') // hello
    Copy the code
  • 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: baz } = { foo: 'aaa'.bar: 'bbb' };
    baz // "aaa"
    foo // error: foo is not defined
    Copy the code
  • Like arrays, deconstruction can also be used for nested structured objects
  • Object destruction can also specify default values, similar to array structures
  • If braces are not written at the beginning of a line, JS will not parse it into a code block
    let x;
    ({x} = {x: 1});// Write it correctly
    {x} = {x: 1};// An error is parsed into a code block
    Copy the code
  • Arrays can be destructively assigned to objects
    let arr = [1.2.3];
    let {0 : first, [arr.length - 1] : last} = arr;
    first / / 1
    last / / 3
    Copy the code

### destruct assignment of string

const [a, b, c, d, e] = 'hello';
Copy the code

### Destruct assignment of values and Boolean values

  • If the right-hand side of the equal sign is a numeric or Boolean value, it is converted to an object first
    let {toString: s} = 123;
    s === Number.prototype.toString // true
    let {toString: s} = true;
    s === Boolean.prototype.toString // true
    Copy the code

    123 and true in the above code have toString attributes when converted to objects, so variable S can be assigned

### Destruct assignment of function arguments

function add([x, y]){
  return x + y;
}
add([1.2]); / / 3
Copy the code
  • Parameter deconstruction can also use default values

There is only one case where parentheses can be used for the non-pattern part of an assignment statement

[(b)] = [3]; / / right
({ p: (d) } = {}); / / right
[(parseInt.prop)] = [3]; / / right
Copy the code

# # #

  • Exchange variable value
let x = 1;
let y = 2;
[x, y] = [y, x];
Copy the code
  • Returns an array or object from a function used to deconstruct the assignment
  • Function parameter definition
  • Extracting JSON objects