Structure assignment of an array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.