1. When the parameters that the user wants to enter are uncertain, the remaining parameters can be used to represent them
const add = (x,y,z,… args) =>{};
2. The nature of remaining parameters
const add = (x, y, ... args) => { console.log(x, y, args); }; add(); //undefined undefined add(1); //1 undefined add(1, 2); // 1 2 [] add(1, 2, 3); [3] Conclusion: The remaining argument is always an array, even if it has no value, it is an empty arrayCopy the code
3. The arrow function cannot omit parentheses even if it has only one remaining argument
4. Use arrow functions instead of arguments to get the actual arguments
``` const sub = function() { console.log(arguments); } sub(); Manufacturer [ƒ, Symbol(Symbol. Iterator): ƒ] sub(1, 2, 3); Manufacturer: ƒ, Symbol(Symbol. Iterator): ƒ] args) => { console.log(args); //[1,2] is an array. subbb(1, 2); ` ` `Copy the code
5. Application of the remaining parameters in practice
// Finish add() const addDemo = (... argss) => { let sum = 0; for (let i = 0; i < argss.length; i++) { sum += argss[i]; } return sum; }; console.log(addDemo()); //0 console.log(addDemo(1, 2, 3, 4)); / / 10Copy the code
6. Use residual arguments in conjunction with deconstructed assignments
const [kkk, ...argsd] = [1, 2, 3, 43, 4];
console.log(kkk, argsd); //1 (4) [2, 3, 43, 4]
Copy the code
7. Array expansion operator
console.log(... [2, 2, 2, 2)); // 2 2 2 2 console.log(Math.min(... [3, 2, 1))); / / 1Copy the code
8. Application of array expansion operators
// Copy array const a = [1, 3, 7]; const c = [...a]; console.log(c); //(3) [1, 3, 7] const b = [1, 2]; const f = [3, 4]; const r = [...b, ...f] console.log(r); //(4) [1, 2, 3, 4] // String converted to array console.log(... 'alexx'); //a l e x x console.log([... 'alexxnb' //(7) ['a', 'l', 'e', 'x', 'x', 'n', 'b'] ]);Copy the code
9. Expansion operators for objects
9.1. Expand Objects: Objects cannot be expanded directly. They must be expanded in {}
Const apple = {color: 'red ', shape:' shape ', taste: 'sweet'}; console.log({... apple }); // New object, {color: 'red ', shape:' spherical ', taste: 'sweet '}Copy the code
9.2 Merge Object: The new object has all attributes, and the latter overwrites the former
Const lizi = {color: 'bai ', shape: 'shape ', taste:' taste '}; Const shizi = {color: 'yellow ', shape:' oval ', taste: 'sweet'}; console.log({... lizi, ... shizi }); //{color: 'yellow ', shape:' oval ', taste: 'sweet '}Copy the code
9.3 The functions of the Object Expansion operator
Const obj1 = {username: 'WWW ', password:' WWWS ', sayHEHE: () => {console.log(); } } const { obj2 } = { ... obj1 } console.log(obj1 === obj2); //falseCopy the code