拓展运算符 rest 参数
// 拓展运算符 把数组或者类数组展开成用逗号隔开的值
// function foo([a, b, c]) {
// console.log(a, b, c);
// }
// let arr = [1, 2, 3]
// foo(arr) // 结构赋值 左边=右边
// function foo(a, b, c) {
// console.log(a, b, c);
// }
// let arr = [1, 2, 3]
// foo(...arr)
// let arr1 = [1, 2, 3]
// let arr2 = [4, 5, 6]
// // es5
// Array.prototype.push.apply(arr1, arr2)
// console.log(arr1);
// // es6
// console.log(arr1.push(...arr2));
// let str = 'imooc'
// var arr = [...str]
// console.log(arr);
// rest 参数
// function foo(x, y, z) {
// // console.log(arguments);
// let sum = 0
// // Array.prototype.forEach.call(arguments, function(item) {
// // sum += item
// // })
// // return sum
// Array.from(arguments).forEach(item => {
// sum += item
// })
// return sum
// }
// console.log(foo(1, 2, 3));
// 当函数参数不确定的时候
// function foo(...args) {
// // console.log(args); // 说明把参数组合成了数组 [1, 2, 3] 与拓展运算符相反
// let sum = 0
// args.forEach(item => {
// sum += item
// })
// return sum
// }
// console.log(foo(1, 2, 3));
// function foo(x, ...args) {
// console.log(x); // 1
// console.log(args); // [2, 3]
// }
// foo(1, 2, 3)
let [x, ...y] = [1, 2, 3]
console.log(x); // 1
console.log(y); // [2, 3]
// 总结
// ... 放在等号的左边 或者 形参上面 是 rest参数
// 等号右边 或者在实参上 是拓展运算符

浙公网安备 33010602011771号