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.