剩余参数
剩余参数是什么
// 1.认识剩余参数// const add = (x, y, z, ...args) => {};// 2.剩余参数的本质const add = (x, y, ...args) => {console.log(x, y, args);};// add();// add(1);// add(1, 2);add(1, 2, 3, 4, 5);// 剩余参数永远是个数组,即使没有值,也是空数组// 3, 4, 5->[3, 4, 5]
剩余参数的注意事项
// 1.箭头函数的剩余参数// 箭头函数的参数部分即使只有一个剩余参数,也不能省略圆括号// const add = (...args) => {};// 2.使用剩余参数替代 arguments 获取实际参数// const add = function () {// console.log(arguments);// };// const add = (...args) => {// console.log(args);// };// add(1, 2);// 3.剩余参数的位置// 剩余参数只能是最后一个参数,之后不能再有其他参数,否则会报错const add = (x, y, ...args) => {console.log(args);};add(1,2,2);
剩余参数的应用
// 1.完成 add 函数// const add = (...args) => {// let sum = 0;// for (let i = 0; i < args.length; i++) {// sum += args[i];// }// // reduce// return sum;// };// // console.log(add());// // console.log(add(1, 1));// console.log(add(1, 2, 3));// 2.与解构赋值结合使用// 剩余参数不一定非要作为函数参数使用// const [num, ...args] = [1, 2, 3, 4];// 必须是最后一个// const [...args,num] = [1, 2, 3, 4];// console.log(num, args);// const func = ([num, ...args]) => {};// func([1, 2, 3]);// const { x, y, ...z } = { a: 3, x: 1, y: 2, b: 4 };// // 必须是最后一个// // const { x, ...z, y } = { a: 3, x: 1, y: 2, b: 4 };// console.log(x, y, z);// const func = ({ x, y, ...z }) => {console.log(z)};// func({ a: 3, x: 1, y: 2, b: 4 });
数组的展开运算符
数组展开运算符的基本用法
// 1.认识展开运算符// [3, 1, 2];// Math.min// console.log(Math.min([3, 1, 2]));// console.log(Math.min(3, 1, 2));// [3, 1, 2]->3, 1, 2// 2.数组展开运算符的基本用法// console.log(Math.min(...[3, 1, 2]));// 相当于// console.log(Math.min(3, 1, 2));
区分剩余参数和展开运算符
…+参数 剩余参数
…+数组 展开运算符
// 1.根本区别// 展开运算符// [3,1,2]->3,1,2// 剩余参数// 3,1,2->[3,1,2]// 2.区分剩余参数和展开运算符// 剩余参数// const add = (...args) => {// // console.log(args);// // 展开运算符// // console.log(...args);// // console.log(...[1, 2, 3]);// console.log(1, 2, 3);// };// add(1, 2, 3);console.log([...[1, 2, 3], 4]);// [1, 2, 3]->1,2,3
数组展开运算符的应用
复制数组
const a = [1, 2];// // const b = a;// // a[0] = 3;// // console.log(b);const c = [...a];// // const c = [1, 2];a[0] = 3;console.log(a);console.log(c);
合并数组
const a = [1, 2];const b = [3];const c = [4, 5];console.log([...a, ...b, ...c]);console.log([...b, ...a, ...c]);console.log([1, ...b, 2, ...a, ...c, 3]);
字符串转为数组
// 字符串可以按照数组的形式展开console.log(...'alex');console.log('a', 'l', 'e', 'x');console.log([...'alex']);console.log('alex'.split(''));// reverse
常见的类数组转化为数组
// argumentsfunction func() {console.log(arguments.push);console.log([...arguments]);}func(1, 2);// NodeListconsole.log(document.querySelectorAll('p'));console.log([...document.querySelectorAll('p')].push);
对象的展开运算符
对象展开运算符的基本用法
// 1.展开对象// 对象不能直接展开,必须在 {} 中展开// const apple = {// color: '红色',// shape: '球形',// taste: '甜'// };// // console.log(...apple);// // console.log([...apple]);// // 对象的展开:把属性罗列出来,用逗号分隔,放到一个 {} 中,构成新对象// console.log({ ...apple });// console.log({ ...apple } === apple);// 2.合并对象const apple = {color: '红色',shape: '球形',taste: '甜'};const pen = {color: '黑色',shape: '圆柱形',use: '写字'};console.log({ ...pen });console.log({ ...apple, ...pen });// // 新对象拥有全部属性,相同属性,后者覆盖前者// // console.log({ ...pen, ...apple });// // 相当于// // console.log({// // use: '写字',// // color: '红色',// // shape: '球形',// // taste: '甜'// // });console.log({ pen, apple });console.log({ ...pen, apple });
对象展开运算符的注意事项
// 1.空对象的展开// 如果展开一个空对象,则没有任何效果// console.log({ ...{} });// console.log({ ...{}, a: 1 });// 2.非对象的展开// 如果展开的不是对象,则会自动将其转为对象,再将其属性罗列出来// console.log({ ...1 });// console.log(new Object(1));// console.log({ ...undefined });// console.log({ ...null });// console.log({ ...true });// 如果展开运算符后面是字符串,它会自动转成一个类似数组的对象,因此返回的不是空对象// console.log({ ...'alex' });// console.log([...'alex']);// console.log(...'alex');// console.log({ ...[1, 2, 3] });// 3.对象中对象属性的展开// 不会展开对象中的对象属性const apple = {feature: {taste: '甜'}};const pen = {feature: {color: '黑色',shape: '圆柱形'},use: '写字'};console.log({ ...apple });console.log({ ...apple, ...pen });// // 相当于// console.log({// feature: {// color: '黑色',// shape: '圆柱形'// },// use: '写字'// });
