๐ก ๋์ ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํต์ฌ ๊ฐ์ด๋๋ฅผ ์ฝ๊ณ ์ ๋ฆฌํฉ๋๋ค.
9.1 The Spread operator (์คํ๋ ๋ ์ฐ์ฐ์)
spread
๋ผ๋ ๋จ์ด๊ฐ ๊ฐ์ง๊ณ ์๋ ์๋ฏธ๋ ํผ์น๋ค, ํผ๋จ๋ฆฌ๋ค- ์ด ๋ฌธ๋ฒ์ ์ฌ์ฉํ๋ฉด ์ดํฐ๋ฌ๋ธํ ์์๋ฅผ ๊ฐ๋ณ ์์๋ก ๋ถ๋ฆฌํ๊ฑฐ๋, ๊ฐ์ฒด ํน์ ๋ฐฐ์ด์ ํผ์น ์ ์์
- ๊ธฐ์กด์ ๋ฐฐ์ด ํน์ ๊ฐ์ฒด๋ฅผ ๊ฑด๋๋ฆฌ์ง ์๊ณ ์๋ก์ด ๋ฐฐ์ด ๋ฐ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผ ํ๋ ๊ฒฝ์ฐ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์์ → ๊ธฐ์กด์ ๊ฒ์ ๊ฑด๋๋ฆฌ์ง ์๊ณ ์๋ก์ด ๊ฒ์ ๋ง๋ ๋ค๋ ๊ฒ์ด ํต์ฌ
// ...[1, 2, 3]์ [1, 2, 3]์ ๊ฐ๋ณ ์์๋ก ๋ถ๋ฆฌํ๋ค(→ 1, 2, 3)
console.log(...[1, 2, 3]); // 1 2 3
// ๋ฌธ์์ด์ ์ดํฐ๋ฌ๋ธ์ด๋ค.
console.log(...'Hello'); // H e l l o
// ์ดํฐ๋ฌ๋ธ์ด ์๋ ์ผ๋ฐ ๊ฐ์ฒด๋ ์คํ๋ ๋ ๋ฌธ๋ฒ์ ๋์์ด ๋ ์ ์๋ค.
console.log(...{ a: 1, b: 2 });
// TypeError: Found non-callable @@iterator
// ์คํ๋ ๋ ์ฐ์ฐ์์ ๊ฒฐ๊ณผ๋ฌผ์ ๊ฐ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค. ์ค์ง ์ผํ๋ก ๊ตฌ๋ถํ ๊ฐ์ ๋ชฉ๋ก์ ์ฌ์ฉํ๋ ๋ฌธ๋งฅ ๋ด์ ๊ฐ์ผ๋ก๋ง ์ฌ์ฉํ ์ ์๋ค.
const list = ...[1, 2, 3]; // SyntaxError: Unexpected token ...
๋ฐฐ์ด์ ๊ฒฐํฉ
const veggie = ["tomato","cucumber","beans"];
const meat = ["pork","beef","chicken"];
const menu = [...veggie, "pasta", ...meat];
console.log(menu);
// Array [ "tomato", "cucumber", "beans", "pasta", "pork", "beef", "chicken" ]
๋ฐฐ์ด์ ๋ณต์ฌ
๋ฐฐ์ด์ ์ฐธ์กฐ
ํ๋จ์ ์ฝ๋๋ ๋ฐฐ์ด์ ๋ณต์ฌํ ๊ฒ ๊ฐ์ ๋ณด์ด์ง๋ง, ์ค์ ๋ก newVeggie๋ ๋ฐฐ์ด veggie๋ฅผ ์ฐธ์กฐํ ๋ฟ์ด๋ค.
const veggie = ["tomato","cucumber","beans"];
const newVeggie = veggie;
veggie.push("peas");
console.log(veggie);
// Array [ "tomato", "cucumber", "beans", "peas" ]
console.log(newVeggie);
// Array [ "tomato", "cucumber", "beans", "peas" ]
๋ฐฐ์ด์ ๋ณต์ฌ : ES5 - concat ์ฌ์ฉ
const veggie = ["tomato","cucumber","beans"];
const newVeggie = [].concat(veggie);
veggie.push("peas");
console.log(veggie);
// Array [ "tomato", "cucumber", "beans", "peas" ]
console.log(newVeggie);
// Array [ "tomato", "cucumber", "beans" ]
๋ฐฐ์ด์ ๋ณต์ฌ : ES6 - spread ์ฐ์ฐ์ ์ฌ์ฉ
const veggie = ["tomato","cucumber","beans"];
const newVeggie = [...veggie];
veggie.push("peas");
console.log(veggie);
// Array [ "tomato", "cucumber", "beans", "peas" ]
console.log(newVeggie);
// Array [ "tomato", "cucumber", "beans" ]
๊ฐ์ฒด ๋ฆฌํฐ๋ด๊ณผ ์คํ๋ ๋ ์ฐ์ฐ์
const slime = {
name: '์ฌ๋ผ์'
};
const cuteSlime = {
...slime,
attribute: 'cute'
};
const purpleCuteSlime = {
...cuteSlime,
color: 'purple'
};
console.log(slime); // Object {name: '์ฌ๋ผ์'}
console.log(cuteSlime); // Object {name: '์ฌ๋ผ์', attribute: 'cute'}
console.log(purpleCuteSlime); // Object {name: '์ฌ๋ผ์', attribute: 'cute', color: 'purple'}
ํจ์ ์ธ์์ ์คํ๋ ๋ ์ฐ์ฐ์
๋ฐฐ์ด ์์ ์๋ ์์๋ค์ ๋ชจ๋ ํจ์์ ์ธ์๋ก ๋ฃ์ด์ฃผ๊ณ ์ถ์ ๋ ์ฌ์ฉ
const numbers = [1, 2, 3, 4, 5, 6];
function sum(...rest) {
return rest.reduce((acc, current) => acc + current, 0);
}
// ES5
const result = sum(
numbers[0],
numbers[1],
numbers[2],
numbers[3],
numbers[4],
numbers[5]
);
console.log(result);
// ES5 - ํจ์ ํธ์ถ, ์ธ์ ์ ๋ฌ
const result = sum.apply(null, numbers);
console.log(result);
// ES6
const numbers = [1, 2, 3, 4, 5, 6];
const result = sum(...numbers);
console.log(result);
9.2 Rest Parameter (๋๋จธ์ง ํ๋ผ๋ฏธํฐ)
- ํ์ฅํ๋ ์ญํ ์ ํ๋ Spread์๋ ๋ฐ๋๋ก, ์ฌ๋ฌ ์์๋ฅผ ํ๋์ ์์๋ก ์์ถํ๋ ์ญํ
- ๊ฐ์ฒด, ๋ฐฐ์ด, ๊ทธ๋ฆฌ๊ณ ํจ์์ ํ๋ผ๋ฏธํฐ์์ ์ฌ์ฉ ๊ฐ๋ฅ
- ํค์๋๊ฐ ๊ผญ rest์ผ ํ์๋ ์์ผ๋ ๊ด์ฉ์ ์ผ๋ก rest๊ฐ ์ฌ์ฉ๋จ
๊ฐ์ฒด์์์ rest
const purpleCuteSlime = {
name: '์ฌ๋ผ์',
attribute: 'cute',
color: 'purple'
};
const { color, ...rest } = purpleCuteSlime;
console.log(color); // purple
console.log(rest); // Object {name: "์ฌ๋ผ์", attribute: "cute"}
ํค์๋, ์ฆ ์ถ์ถํ ๊ฐ์ ์ด๋ฆ์ด ๊ผญ rest์ผ ํ์๊ฐ ์์ผ๋ฏ๋ก ์๋์ ๊ฐ์ด ์ฌ์ฉ ๊ฐ๋ฅ
const purpleCuteSlime = {
name: '์ฌ๋ผ์',
attribute: 'cute',
color: 'purple'
};
const { color, ...cuteSlime } = purpleCuteSlime;
console.log(color); // purple
console.log(cuteSlime); // Object {name: "์ฌ๋ผ์", attribute: "cute"}
const { attribute, ...slime } = cuteSlime;
console.log(attribute); // cute
console.log(slime); // Object {name: "์ฌ๋ผ์"}
๋ฐฐ์ด์์์ rest
const numbers = [0, 1, 2, 3, 4, 5, 6];
const [one, ...rest] = numbers;
console.log(one);
console.log(rest);
rest ํ๋ผ๋ฏธํฐ๋ ์ฌ์ฉ ์ ๊ผญ ๋ง์ง๋ง์ ์์นํด์ผ ํ๋ฏ๋ก ์๋์ ๊ฐ์ด ์ฌ์ฉ ๋ถ๊ฐ๋ฅ
const numbers = [0, 1, 2, 3, 4, 5, 6];
const [..rest, last] = numbers;
ํจ์ ํ๋ผ๋ฏธํฐ์์์ rest
ํจ์์ ํ๋ผ๋ฏธํฐ๊ฐ ๋ช ๊ฐ๊ฐ ๋ ์ง ๋ชจ๋ฅด๋ ์ํฉ์์ rest ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ๋งค์ฐ ์ ์ฉ
// ES5
function sum(a, b, c, d, e, f, g) {
let sum = 0;
if (a) sum += a;
if (b) sum += b;
if (c) sum += c;
if (d) sum += d;
if (e) sum += e;
if (f) sum += f;
if (g) sum += g;
return sum;
}
const result = sum(1, 2, 3, 4, 5, 6);
console.log(result);
// ES6
function sum(...rest) {
return rest.reduce((acc, current) => acc + current, 0);
}
const result = sum(1, 2, 3, 4, 5, 6);
console.log(result); // 21
9.3 Spread์ Rest์ ์ฐจ์ด
Spread
- Spread๋ ๊ธฐ์กด ์ฅ์์ ์๋ ์์๋ฅผ ์๋ก์ด ์ฅ์์ ํ์ด๋ด์ ์ฌ์ฉํ๋ ๊ฒ
- ์ฌ์ฉ ์์น(์์) ์๊ด ์์
- ํจ์์์ ์ฌ์ฉ ์, ํจ์์ ์ธ์ ์๋ฆฌ์๋ง ์ฌ์ฉ ๊ฐ๋ฅ
Rest
- Rest๋ ๊ฐ์ ์ฅ์ ์์์ ์ง์ ์ ์ธํ ๋ณ์ ๋ฑ์ ํน์ ํ ๊ฒ๋ค์ ์ ์ธํ ๋๋จธ์ง ๊ฐ์ ๋ฌถ์ด์ฃผ๋ ๊ฒ
- ์ฌ์ฉ ์ ๋ ๋ง์ง๋ง์ ์์นํด์ผ ํจ
- ํจ์์์ ์ฌ์ฉ ์, ํจ์์ ์ธ์ ์๋ฆฌ์๋ง ์ฌ์ฉ ๊ฐ๋ฅ
์์ ์ฝ๋
function sum(...rest) { // ์ธ์์ ์ฌ์ฉ๋ ...์ rest
return rest.reduce((acc, current) => acc + current, 0);
}
const numbers = [1, 2, 3, 4, 5, 6];
const result = sum(...numbers); // ์ธ์์ ์ฌ์ฉ๋ ...์ spread
console.log(result);
๐ ์ฐธ๊ณ
'Tech > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Chapter 11: ์๋ฐ์คํฌ๋ฆฝํธ ์ฌ๋ณผ (Symbols) (0) | 2022.01.25 |
---|---|
Chapter 10: ๊ฐ์ฒด ๋ฆฌํฐ๋ด (Object literal upgrades) (0) | 2022.01.24 |
Chapter 08: ๋ฐฐ์ด ๋ฉ์๋ (Array improvements) (0) | 2022.01.22 |
Chapter 07: ๋ฐ๋ณต๋ฌธ (Loop) (0) | 2022.01.20 |
Chapter 06: ๋์คํธ๋ญ์ฒ๋ง - ๊ตฌ์กฐ ๋ถํด ํ ๋น (Destructuring) (0) | 2022.01.19 |