... Operator
Convert an array into a comma-separated parameter sequence.
Function push (array ,... items) {array. push (... items);} function add (x, y) {return X + Y;} VAR numbers = [4, 38]; add (... numbers );
Function calls with variable parameters
Function push (array ,... items) {array. push (... items);} function add (... vals) {Let sum = 0; For (let I = 0; I <Vals. length; I ++) {sum + = Vals [I];} return sum;} Let arr = [1, 2, 4, 5, 6]; let sum = add (... ARR); console. log (SUM); // 21
More convenient array Merging
Let arr1 = [1, 2]; let arr2 = [5, 6]; let newarr = [20]; // newarr = newarr in the old es5 statement. concat (arr1 ). concat (arr2); // [20, 1, 2, 5, 6] Console. log (newarr); // es6 use the extension operator newarr = [20 ,... arr1 ,... arr2]; // [20, 1, 2, 5, 6] Console. log (newarr );
Replace the apply method of es5
// Es5 function f (x, y, z ){//...} vaR ARGs = [0, 1, 2]; F. apply (null, argS); // function f (x, y, z) Written in es6 ){//...} vaR ARGs = [0, 1, 2]; F (... ARGs );
Maximum math. Max ()
// Es5 statement math. max. apply (null, [14, 3, 77]) // write math for es6. max (... [14, 3, 77]) // equivalent to math. max (14, 3, 77 );
Add an array to the end of another array through the push function
// Es5 syntax var arr1 = [0, 1, 2]; var arr2 = [3, 4, 5]; array. prototype. push. apply (arr1, arr2); // write var arr1 = [0, 1, 2]; var arr2 = [3, 4, 5]; arr1.push (... arr2 );
New date type
// Es5 new (date. BIND. apply (date, [null, 2015, 1, 1]) // es6 new date (... [2015, 1, 1]);
Combined with deconstruct assignment to generate a new array
// Es5 A = list [0], rest = List. Slice (1) // es6 [A,... rest] = List
The following are some other examples.
Const [first ,... rest] = [1, 2, 3, 4, 5]; first/1 rest // [2, 3, 4, 5] const [first ,... rest] = []; first // undefined rest // []: const [first ,... rest] = ["foo"]; first // "foo" Rest // []
Convert a string to a real Array
[... 'Hello'] // ["H", "E", "L", "L", "O"]
Convert objects that implement the iterator interface into Arrays
VaR nodelist = Document. queryselectorall ('div '); var array = [... nodelist];
New es6 knowledge