The following is sorted out from the Nanyi of "ECMAScript 6 ," chapter III the use of the assignment of the value of the deconstruction of the variables array
Let [A, B, c] = [1, 2, 3];
let [foo = true] = [];
Object
Let {foo, bar} = {foo: "AAA", Bar: "BBB"};
let {Foo:baz} = {foo: ' aaa ', bar: ' BBB '};
String
const [A, B, C, D, e] = ' Hello ';
Function
function Add ([x, Y]) {return
x + y;
}
Add ([1, 2]); 3
[[1, 2], [3, 4]].map ([A, b]) => A + B);
function Move ({x = 0, y = 0} = {}) {return
[x, y];
}
Move ({x:3, y:8}); [3, 8]
The purpose of the deconstruction assignmentValue of the Exchange variable
Let x = 1;
Let y = 2;
[x, Y] = [y, x];
Returning multiple values from a function
function Example () {return
[1, 2, 3];
}
Let [A, B, c] = example ();
Function app () {return
{
foo:1,
bar:2
};
}
Let {foo, bar} = App ();
Definition of function parameters
function f ([x, Y, z]) {...}
f ([1, 2, 3]);
function g ({x, y, z}) {...}
G ({z:3, y:2, x:1});
Extract JSON data
Let Jsondata = {
id:42,
status: ' OK ',
data: [867, 5309]
};
Let {ID, status, data:number} = Jsondata;
Default values for function arguments
Jquery.ajax = function (URL, {
async = true,
beforesend = function () {},
cache = True,
complete = funct Ion () {},
Crossdomain = False,
global = True,
//... more config
}) {
//... do stuff
};
Traversing the map structure
var map = new map ();
Map.set (' A ', ' hello ');
Map.set (' Second ', ' World ');
for (let [key, value] of map) {
Console.log (key + ' is ' + value);
}
for (let [key] of map) {
console.log (key);
}
for (let [, value] of map) {
Console.log (value);
}
Input module's specified method
const {Sourceconsumer, Sourcenode} = require (' Source-map ');
The above content is sorted out from the Nanyi "ECMAScript 6 Getting Started" the third chapter variable's deconstruction assignment