New Feature 4 of ES6: deconstruct and value assignment of variables and new features of es6

Source: Internet
Author: User

New Feature 4 of ES6: deconstruct and value assignment of variables and new features of es6

This article describes the deconstruct and assignment of new features of ES6. We will share this with you for your reference. The details are as follows:

1. Deconstruct assignment of Arrays

// ① Extract values from the array and assign values to the variable var [a, B] = [1, 2] According to the corresponding position; // a = 1; B = 2 // ② The following describes how to use nested arrays to deconstruct var [d, [[c], f] = [1, [2], 3]; var [, third] = ["foo", "bar", "baz"]; // third = "baz" var [head ,... tail] = [1, 2, 3, 4]; // head = 1; tail = [2, 3, 4] // ③ incomplete structure var [x, y] = [, 6]; // x = 1; y = 2 // ④ deconstruct value assignment allows you to specify the default value var [foo = true] = []; // foo = truevar [x, y = 'B'] = ['a'] // x = 'A', y = 'B' var [x, y = 'B'] = ['A', undefined] // x = 'A', y = 'B' // ⑤ the deconstruct fails, the value of the variable is equal to undefinedvar [a6, a7] = [1]; // a7 = undefined

2. Deconstruct value assignment of Set

[a, b, c] = new Set(["a", "b", "c"])console.log(a) // "a"

3. Deconstruct assignment of Objects

// The attributes of the object are not ordered. The variables must have the same name as the attributes before obtaining the correct value var {x, y }={ x: "aaa", y: "bbb "}; // x = aaa, y = bbbbvar {c} = {a: "aaa", B: "bbb "}; // c = undefined // strictly abide by the variable name and the var {d: e }={ d: "aaa", f: "bbb "}; // e = aaa // specify the default value var {x, y = 5 }={ x: 1}; // x = 1, y = 5

4. Used to traverse the Map

Var map = new Map (); map. set ('first ', 'Hello'); map. set ('second', 'World'); // obtain the key-value pair for (let [key, value] of map) {console. log (key + "is" + value);} // obtain the key name for (let [key] of map) {console. log (key) ;}// obtain the value for (let [, value] of map) {console. log (value );}

I hope this article will help you design the ECMAScript program.

Related Article

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.