2021/10/09 09:58

# 1.总览

function f(x, y=12) {
// y is 12 if not passed (or passed as undefined)
return x + y;
}
f(3) == 15

function f(x, ...y) {
// y is an Array
return x * y.length;
}
f(3, "hello", true) == 6

function f(x, y, z) {
return x + y + z;
}
// Pass each elem of array as argument
f(...[1,2,3]) == 6


# 2.展开谈谈

## 2.1.函数形参默认值

function increment(number, increment){
if (typeof increment == 'undefined' ) increment = 1;
return number + increment;
}

increment(1, 2) // 3
increment(1) // 2


function increment(number, increment = 1){
return number + increment;
}

increment(5,5); // 10
increment(5); // 6


## 2.2.压缩多个元素到数组

function add(...numbers){
return numbers.reduce((acc, number) => {
return acc + number;
}, 0);
}

add(1, 2, 3, 4, 5); // 15

add(2, 4, 6, 8, 10); // 30


## 2.3.将数组中的元素铺开

// Array concatenation with spread
const array1 = ['Cersie', 'Jamie'];
const array2 = ['Tyrion'];

const lannisters = [...array1, ...array2];

console.log(lannisters); // ['Cersie' 'Jaime', 'Tyrion']


0
0 收藏

0 评论
0 收藏
0