Tag: string not com font false Comment interface Foo statement
Structure assignment of an array
let a = 1;let b = 2;let c = 3;//ES6 允许写成下面这样。let [a, b, c] = [1, 2, 3];
This is a pattern match, as long as the pattern is the same on both sides of the equal sign, the left variable will be given the corresponding value
nested notation
let [foo, [[Bar], Baz]] = [1, [[2],3]]; Foo1bar2baz3let [,, third] = [ "foo", "bar", "baz"];third //' Baz ' let [x, y] = [1, 2, 3];x // 1y //3let [head, ... tail] = [1, Span class= "Hljs-number" >2, 3, 4];head //1tail //[2, 3, 4]let [x, Y, ... z] = [ ' a '];x //"a" y //Undefinedz Span class= "Hljs-comment" >//[]
If the deconstruction is unsuccessful, the value of the variable is undefined. The following two scenarios are deconstruction unsuccessful, and the value of Foo will be equal to undefined.
let [foo] = [];let [bar, foo] = [1];
The other is completely unsolvable, that the pattern on the left side of the equals sign matches only a subset of the right-hand array of equals, but can still be deconstructed successfully. For example, the following code.
let [x, y] = [1, 2, 3];x // 1y // 2let [a, [b], d] = [1, [2, 3], 4];a // 1b // 2d // 4
If the right side of the equal sign is a non-ergodic deconstruction, an error will be put.
// 报错let [foo] = 1;let [foo] = false;let [foo] = NaN;let [foo] = undefined;let [foo] = null;let [foo] = {};
The above statements will error because they do not have a iterator interface, which means they cannot be traversed.
In fact, as long as a data structure has a Iterator interface, it can be used as an array of deconstruction assignment.
Structure assignment of an array