- 参数扩展/展开
...args
- 收集剩余的参数,必须当到最后一个参数位置
- 展开数组,简写,效果和直接把数组的内容写在这儿一样
- 默认参数
function show (a, b, ...args) {
console.log(a);
console.log(b);
console.log(args);
}
show(12, 15, 18, 20, 22, 60);
function show1 (a, b = 5, c = 12) {
console.log(a, b, c);
}
show1(99);
show1(99, 19, 88);
let arr = [1, 2, 3];
function show2 (a, b, c) {
console.log(a);
console.log(b);
console.log(c);
}
show2(...arr);
function show3 (...args) {
fn(...args)
}
function fn (a, b) {
console.log(a + b);
}
show3(12, 5);
let arr1 = [1, 3, 5, 7, 9]
let arr2 = [2, 4, 6, 8, 10]
console.log(...arr1);
let arr3 = [...arr1, ...arr2]
function sum (...numbers)
{
return numbers.reduce((preValue, currentValue) =>
{
return preValue + currentValue
})
}
console.log(sum(1, 2, 3, 4));
let person = { name: 'tom', age: 18 }
let person2 = { ...person }
person.name = 'jerry'
console.log(person);
console.log(person2);
let person3 = { ...person, name: 'jack', address: "地球" }
console.log(person3);
let { name, ...newPerson } = person3
console.log(newPerson);
let newPerson2 = (({ age, address }) => ({ age, address }))(person3)
console.log(newPerson2);