DAY 20
0
Modern Web

其餘參數（rest parameter）

``````const sum = function () {
let num = 0;
for (var i = 0; i < arguments.length; i++) {
num += arguments[i];
}
return num;
};
sum(3, 4, 5); // 12
``````

`...` 為展開運算子 Spread syntax (...) -MDN
1. block body 寫法
``````const sum = (...rex) => {
let num = 0;
for (let i = 0; i < rex.length; i++) {
num += rex[i];
}
return num;
};
sum(3, 4, 5); // 12
sum(1, 2, 3, 4, 5, 6); // 21
``````

reduce() 方法將一個累加器及陣列中每項元素（由左至右）傳入回呼函式，將陣列化為單一值。-MDN
1. concise body 寫法（讓這個函式更加簡短）
``````const sum = (...args) => args.reduce((a, b) => a + b);
sum(3, 4, 5); // 12
sum(1, 2, 3, 4, 5, 6); // 21
``````

以「物件」作為參數

``````var addPerson = function (firstname, lastname, phone, email, gender, birthday, address) {
// 略..
};
``````

``````addperson("Stephen", "Chow", "+852 3345678", "stephen5566@mail.com", "male", "HK")
``````

``````var person = {
firstname: "Stephen",
lastname: "Chow",
phone: "+852 3345678",
email: "stephen5566@mail.com",
gender: "male",
birthday: "0622",
};
``````