剩余参数

剩余参数是什么

  1. // 1.认识剩余参数
  2. // const add = (x, y, z, ...args) => {};
  3. // 2.剩余参数的本质
  4. const add = (x, y, ...args) => {
  5. console.log(x, y, args);
  6. };
  7. // add();
  8. // add(1);
  9. // add(1, 2);
  10. add(1, 2, 3, 4, 5);
  11. // 剩余参数永远是个数组,即使没有值,也是空数组
  12. // 3, 4, 5->[3, 4, 5]

剩余参数的注意事项

  1. // 1.箭头函数的剩余参数
  2. // 箭头函数的参数部分即使只有一个剩余参数,也不能省略圆括号
  3. // const add = (...args) => {};
  4. // 2.使用剩余参数替代 arguments 获取实际参数
  5. // const add = function () {
  6. // console.log(arguments);
  7. // };
  8. // const add = (...args) => {
  9. // console.log(args);
  10. // };
  11. // add(1, 2);
  12. // 3.剩余参数的位置
  13. // 剩余参数只能是最后一个参数,之后不能再有其他参数,否则会报错
  14. const add = (x, y, ...args) => {
  15. console.log(args);
  16. };
  17. add(1,2,2);

剩余参数的应用

  1. // 1.完成 add 函数
  2. // const add = (...args) => {
  3. // let sum = 0;
  4. // for (let i = 0; i < args.length; i++) {
  5. // sum += args[i];
  6. // }
  7. // // reduce
  8. // return sum;
  9. // };
  10. // // console.log(add());
  11. // // console.log(add(1, 1));
  12. // console.log(add(1, 2, 3));
  13. // 2.与解构赋值结合使用
  14. // 剩余参数不一定非要作为函数参数使用
  15. // const [num, ...args] = [1, 2, 3, 4];
  16. // 必须是最后一个
  17. // const [...args,num] = [1, 2, 3, 4];
  18. // console.log(num, args);
  19. // const func = ([num, ...args]) => {};
  20. // func([1, 2, 3]);
  21. // const { x, y, ...z } = { a: 3, x: 1, y: 2, b: 4 };
  22. // // 必须是最后一个
  23. // // const { x, ...z, y } = { a: 3, x: 1, y: 2, b: 4 };
  24. // console.log(x, y, z);
  25. // const func = ({ x, y, ...z }) => {console.log(z)};
  26. // func({ a: 3, x: 1, y: 2, b: 4 });

数组的展开运算符

数组展开运算符的基本用法

  1. // 1.认识展开运算符
  2. // [3, 1, 2];
  3. // Math.min
  4. // console.log(Math.min([3, 1, 2]));
  5. // console.log(Math.min(3, 1, 2));
  6. // [3, 1, 2]->3, 1, 2
  7. // 2.数组展开运算符的基本用法
  8. // console.log(Math.min(...[3, 1, 2]));
  9. // 相当于
  10. // console.log(Math.min(3, 1, 2));

区分剩余参数和展开运算符

…+参数 剩余参数
…+数组 展开运算符

  1. // 1.根本区别
  2. // 展开运算符
  3. // [3,1,2]->3,1,2
  4. // 剩余参数
  5. // 3,1,2->[3,1,2]
  6. // 2.区分剩余参数和展开运算符
  7. // 剩余参数
  8. // const add = (...args) => {
  9. // // console.log(args);
  10. // // 展开运算符
  11. // // console.log(...args);
  12. // // console.log(...[1, 2, 3]);
  13. // console.log(1, 2, 3);
  14. // };
  15. // add(1, 2, 3);
  16. console.log([...[1, 2, 3], 4]);
  17. // [1, 2, 3]->1,2,3

数组展开运算符的应用

复制数组

  1. const a = [1, 2];
  2. // // const b = a;
  3. // // a[0] = 3;
  4. // // console.log(b);
  5. const c = [...a];
  6. // // const c = [1, 2];
  7. a[0] = 3;
  8. console.log(a);
  9. console.log(c);

合并数组

  1. const a = [1, 2];
  2. const b = [3];
  3. const c = [4, 5];
  4. console.log([...a, ...b, ...c]);
  5. console.log([...b, ...a, ...c]);
  6. console.log([1, ...b, 2, ...a, ...c, 3]);

字符串转为数组

  1. // 字符串可以按照数组的形式展开
  2. console.log(...'alex');
  3. console.log('a', 'l', 'e', 'x');
  4. console.log([...'alex']);
  5. console.log('alex'.split(''));
  6. // reverse

常见的类数组转化为数组

  1. // arguments
  2. function func() {
  3. console.log(arguments.push);
  4. console.log([...arguments]);
  5. }
  6. func(1, 2);
  7. // NodeList
  8. console.log(document.querySelectorAll('p'));
  9. console.log([...document.querySelectorAll('p')].push);

对象的展开运算符

对象展开运算符的基本用法

  1. // 1.展开对象
  2. // 对象不能直接展开,必须在 {} 中展开
  3. // const apple = {
  4. // color: '红色',
  5. // shape: '球形',
  6. // taste: '甜'
  7. // };
  8. // // console.log(...apple);
  9. // // console.log([...apple]);
  10. // // 对象的展开:把属性罗列出来,用逗号分隔,放到一个 {} 中,构成新对象
  11. // console.log({ ...apple });
  12. // console.log({ ...apple } === apple);
  13. // 2.合并对象
  14. const apple = {
  15. color: '红色',
  16. shape: '球形',
  17. taste: '甜'
  18. };
  19. const pen = {
  20. color: '黑色',
  21. shape: '圆柱形',
  22. use: '写字'
  23. };
  24. console.log({ ...pen });
  25. console.log({ ...apple, ...pen });
  26. // // 新对象拥有全部属性,相同属性,后者覆盖前者
  27. // // console.log({ ...pen, ...apple });
  28. // // 相当于
  29. // // console.log({
  30. // // use: '写字',
  31. // // color: '红色',
  32. // // shape: '球形',
  33. // // taste: '甜'
  34. // // });
  35. console.log({ pen, apple });
  36. console.log({ ...pen, apple });

对象展开运算符的注意事项

  1. // 1.空对象的展开
  2. // 如果展开一个空对象,则没有任何效果
  3. // console.log({ ...{} });
  4. // console.log({ ...{}, a: 1 });
  5. // 2.非对象的展开
  6. // 如果展开的不是对象,则会自动将其转为对象,再将其属性罗列出来
  7. // console.log({ ...1 });
  8. // console.log(new Object(1));
  9. // console.log({ ...undefined });
  10. // console.log({ ...null });
  11. // console.log({ ...true });
  12. // 如果展开运算符后面是字符串,它会自动转成一个类似数组的对象,因此返回的不是空对象
  13. // console.log({ ...'alex' });
  14. // console.log([...'alex']);
  15. // console.log(...'alex');
  16. // console.log({ ...[1, 2, 3] });
  17. // 3.对象中对象属性的展开
  18. // 不会展开对象中的对象属性
  19. const apple = {
  20. feature: {
  21. taste: '甜'
  22. }
  23. };
  24. const pen = {
  25. feature: {
  26. color: '黑色',
  27. shape: '圆柱形'
  28. },
  29. use: '写字'
  30. };
  31. console.log({ ...apple });
  32. console.log({ ...apple, ...pen });
  33. // // 相当于
  34. // console.log({
  35. // feature: {
  36. // color: '黑色',
  37. // shape: '圆柱形'
  38. // },
  39. // use: '写字'
  40. // });

对象展开运算符的应用