07. spread ์™€ rest

07. spread ์™€ rest

์ด๋ฒˆ์—๋Š” ES6 ์—์„œ ๋„์ž…๋œ spread ์™€ rest ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์„œ๋กœ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋ฌธ๋ฒ•์ธ๋ฐ์š”, ์€๊ทผํžˆ ์ข€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค.

spread

์ผ๋‹จ spread ๋ฌธ๋ฒ•๋ถ€ํ„ฐ ์•Œ์•„๋ด…์‹œ๋‹ค. spread ๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์˜๋ฏธ๋Š” ํŽผ์น˜๋‹ค, ํผ๋œจ๋ฆฌ๋‹ค ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด, ๊ฐ์ฒด ํ˜น์€ ๋ฐฐ์—ด์„ ์†์‰ฝ๊ฒŒ ์ปจํŠธ๋กคํ• ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋“ค์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค.

const slime = {
  name: '์Šฌ๋ผ์ž„'
};

const cuteSlime = {
  name: '์Šฌ๋ผ์ž„',
  attribute: 'cute'
};

const purpleCuteSlime = {
  name: '์Šฌ๋ผ์ž„',
  attribute: 'cute',
  color: 'purple'
};

console.log(slime);
console.log(cuteSlime);
console.log(purpleCuteSlime);
// ๊ฒฐ๊ณผ)
Object {name: '์Šฌ๋ผ์ž„'}
Object {name: '์Šฌ๋ผ์ž„', attribute: 'cute'}
Object {name: '์Šฌ๋ผ์ž„', attribute: 'cute', color: 'purple'}

์ด ์ฝ”๋“œ์—์„œ๋Š” ๋จผ์ € slime ์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  cuteSlime ์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ์š”, ๊ธฐ์กด์— ์„ ์–ธํ•œ slime ์„ ๊ฑด๋“ค์ด์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ slime ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.(์ฆ‰ ๋จผ์ €์„ ์–ธํ•œ ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•ด์„œ ๊ฐ€์ ธ์˜จ๊ฒƒ ์ž…๋‹ˆ๋‹ค.)

๊ทธ ๋‹ค์Œ์—๋Š” purpleCuteSlime ์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ์š”, ์ด ๊ฐ์ฒด๋Š” cuteSlime ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์†์„ฑ์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ถ”๊ฐ€์ ์œผ๋กœ color ๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์œ„ ์ฝ”๋“œ์—์„œ์˜ ํ•ต์‹ฌ์€, ๊ธฐ์กด์˜ ๊ฒƒ์„ ๊ฑด๋“ค์ด์ง€ ์•Š๊ณ , ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ ์ธ๋ฐ์š”, ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์— ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์šฉํ•œ ๋ฌธ๋ฒ•์ด spread ์ž…๋‹ˆ๋‹ค.

์•„๊นŒ ์ฝ”๋“œ๋Š” spread ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const slime = {
  name: '์Šฌ๋ผ์ž„'
};

const cuteSlime = {
  ...slime,
  attribute: 'cute'
};

const purpleCuteSlime = {
  ...cuteSlime,
  color: 'purple'
};

console.log(slime);
console.log(cuteSlime);
console.log(purpleCuteSlime);
// ๊ฒฐ๊ณผ)
{name: "์Šฌ๋ผ์ž„"}
{name: "์Šฌ๋ผ์ž„", attribute: "cute"}
{name: "์Šฌ๋ผ์ž„", attribute: "cute", color: "purple"}

์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•œ ... ๋ฌธ์ž๊ฐ€ ๋ฐ”๋กœ spread ์—ฐ์‚ฐ์ž์ž…๋‹ˆ๋‹ค.

spread ์—ฐ์‚ฐ์ž ๋ฐฐ์—ด์—์„œ๋„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const animals = ['๊ฐœ', '๊ณ ์–‘์ด', '์ฐธ์ƒˆ'];
const anotherAnimals = [...animals, '๋น„๋‘˜๊ธฐ'];
console.log(animals);
console.log(anotherAnimals);
// ๊ฒฐ๊ณผ)
['๊ฐœ','๊ณ ์–‘์ด','์ฐธ์ƒˆ']
['๊ฐœ','๊ณ ์–‘์ด','์ฐธ์ƒˆ', '๋น„๋‘˜๊ธฐ']

๊ธฐ์กด์˜ animals ๋Š” ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š์œผ๋ฉด์„œ, ์ƒˆ๋กœ์šด anotherAnimals ๋ฐฐ์—ด์— animals ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋‚ด์šฉ์„ ๋ชจ๋‘ ์ง‘์–ด๋„ฃ๊ณ , '๋น„๋‘˜๊ธฐ' ๋ผ๋Š” ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€์ ์œผ๋กœ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค.

๋ฐฐ์—ด์—์„œ spread ์—ฐ์‚ฐ์ž๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ์‚ฌ์šฉ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

const numbers = [1, 2, 3, 4, 5];

const spreadNumbers = [...numbers, 1000, ...numbers];
console.log(spreadNumbers); // [1, 2, 3, 4, 5, 1000, 1, 2, 3, 4, 5]

rest

rest๋Š” ์ƒ๊น€์ƒˆ๋Š” spread ๋ž‘ ๋น„์Šทํ•œ๋ฐ, ์—ญํ• ์ด ๋งค์šฐ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. rest๋Š” ๊ฐ์ฒด, ๋ฐฐ์—ด, ๊ทธ๋ฆฌ๊ณ  ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๊ฐ์ฒด์—์„œ์˜ rest

์šฐ์„  ๊ฐ์ฒด์—์„œ์˜ ์˜ˆ์‹œ๋ฅผ ์•Œ์•„๋ณผ๊นŒ์š”?

const purpleCuteSlime = {
  name: '์Šฌ๋ผ์ž„',
  attribute: 'cute',
  color: 'purple'
};

const { color, ...rest } = purpleCuteSlime;
console.log(color);
console.log(rest);
// ๊ฒฐ๊ณผ)
purple
Object {name: '์Šฌ๋ผ์ž„', attribute: 'cute'}

rest ์•ˆ์— color ๊ฐ’์„ ์ œ์™ธํ•œ ๊ฐ’์ด ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค.

rest ๋Š” ๊ฐ์ฒด์™€ ๋ฐฐ์—ด์—์„œ ์‚ฌ์šฉ ํ•  ๋•Œ๋Š” ์ด๋ ‡๊ฒŒ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ๋ฌธ๋ฒ•๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฃผ๋กœ ์‚ฌ์šฉ ํ• ๋•Œ๋Š” ์œ„์™€ ๊ฐ™์ด rest ๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ์š”, ์ถ”์ถœํ•œ ๊ฐ’์˜ ์ด๋ฆ„์ด ๊ผญ rest ์ผ ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.

const tion = (...ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค) => {
     ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค.forEach(element => {
        console.log(element)
    });
};

tion(1,2,3,4,5);
// ๊ฒฐ๊ณผ)
1
2
3
4
5

Last updated