1 Extension operator
In the ES6. Three dots (…). There are two meanings. Represents the extended and residual operators, respectively.
The array a
function test(a,b,c){ console.log(a); console.log(b); console.log(c); } var arr = [1, 2, 3]; test(... arr); // Prints the result // 1 // 2 // 3Copy the code
Inserts an array into another data
var arr1 = [1, 2, 3]; var arr2 = [...arr1, 4, 5, 6]; console.log(arr2); // Print the result // [1, 2, 3, 4, 5, 6]Copy the code
String to data
var str='test'; var arr3= [...str]; console.log(arr3); // ["t", "e", "s", "t"]Copy the code
2 Residual operator
The rest operator is used when the number of function arguments is uncertain
function rest1(... arr) { for (let item of arr) { console.log(item); } } rest1(1, 2, 3); // Prints the result // 1 // 2 // 3Copy the code
The second case when the number of parameters of the function is uncertain
function rest2(item, ... arr) { console.log(item); console.log(arr); } rest2(1, 2, 3, 4, 5); // print the result // 1 // [2, 3, 4, 5]Copy the code
Deconstruction use
var [a,...temp]=[1, 2, 3, 4]; console.log(a); console.log(temp); [2, 3, 4]Copy the code