์คํ๋ ๋ ์ฐ์ฐ์('...')๋ ์ฐ๊ฒฐ, ๋ณต์ฌ ๋ฑ์ ์ฉ๋๋ก ์ฌ์ฉ๋๋ฉฐ ๋ฐฐ์ด๊ณผ ๊ฐ์ฒด๋ฅผ ํ ์ค๋ก ํผ์น๋ ๋ฐฉ๋ฒ์ด๋ผ๊ณ ํ ์ ์๋ค.
๋ฐฐ์ด์์๋ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
const coffee = ["์๋ฉ๋ฆฌ์นด๋
ธ", "๋ฐ๋๋ผ๋ผ๋ผ"];
const juice = ["ํฌ๋์ฃผ์ค", "์ค๋ ์ง์ฃผ์ค", "ํ ๋งํ ์ฃผ์ค"];
const menu = [...coffee, "์๋ก์ด ์๋ฃ", ...juice];
console.log(menu); // ["์๋ฉ๋ฆฌ์นด๋
ธ", "๋ฐ๋๋ผ๋ผ๋ผ", "์๋ก์ด ์๋ฃ", "ํฌ๋์ฃผ์ค", "์ค๋ ์ง์ฃผ์ค", "ํ ๋งํ ์ฃผ์ค"]
์๋นํ ์ง๊ด์ ์ธ ์์๋ผ๊ณ ์๊ฐํ๋ค ^-^!
๊ฐ์ฒด์์๋ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
const latte = {
base: "milk",
from: "Starbucks"
};
const banillaLatte = {
...latte,
syrup: "banilla"
};
console.log(banillaLatte); // { base: 'milk', from: 'Starbucks', syrup: 'banilla' }
์ฆ, ์ค๋ณต๋ ํ๋กํผํฐ๋ฅผ ๊ณ์ ์์ฑํด์ผํ ๋ spread์ ์ฌ์ฉ์ผ๋ก ๊ฐ๋จํ๊ฒ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค.
ํจ์์์๋ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
function add(...param) {
let sum = 0;
for (let n of param) {
sum += n;
}
return sum;
}
console.log(add(1)); // 1
console.log(add(1,2)); // 3
console.log(add(1,2,3)); // 6
spread ์ฐ์ฐ์ + ์ ํด์ง์ง ์์ ์ธ์๋ฅผ ๋ฐฐ์ด๋ก ๋ํ๋ผ ์ ์๊ฒํ๋ rest parameter์ ์กฐํฉ์ผ๋ก๋ช ๊ฐ์ ์ธ์๊ฐ ์ค๋ ์ง, ๋ชจ๋ ์ธ์๋ฅผ ๋ํ๋ ํจ์๋ฅผ ๊ตฌํํ ์ ์๋ค.
* ์ฐธ๊ณ ๋ก ์ด ๋ ๋ค์ด์ค๋ param์ ๋ฐฐ์ด์ด๋ค.
'๐ฆ Computer Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] Promise | ์ฝ๋ฐฑ์ง์ฅ์ ํ์ถํ๋ ๋ฐฉ๋ฒ (0) | 2022.03.11 |
---|---|
[JavaScript] ๋๊ธฐ & ๋น๋๊ธฐ (0) | 2022.03.04 |
[JavaScript] ๋น๊ตฌ์กฐํ ํ ๋น ๊ทธ๋ฆฌ๊ณ SWAP (0) | 2022.03.03 |
[JavaScript] Truthy & Falsy (0) | 2022.02.24 |
[JavaScript] ๋ฐฐ์ด ๋ด์ฅ ํจ์ (0) | 2022.02.21 |