์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ• ... ์„ ์ด์šฉํ•œ ์ „๊ฐœ ๊ตฌ๋ฌธ(spread syntax)

IT/Javascript

by J KIMS 2021. 1. 28. 16:39

๋ณธ๋ฌธ

๋ฐ˜์‘ํ˜•

 

๐Ÿ”ฅ Javascript Spread Syntax (..)

 

๋‹ค๋ฅธ ๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ค๊ฐ€ ๋ฐฐ์—ด์•ˆ์— ... ์„ ์‚ฌ์šฉํ•œ ๊ฑธ ๋ณด๊ณ  ๋ญ”๊ฐ€ ์‹ถ์–ด์„œ ์ฐพ์•„๋ณด๋‹ˆ ์ „๊ฐœ ๊ตฌ๋ฌธ(Spread syntax)๋ผ ๋ถˆ๋ฆฌ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์ด ์žˆ์—ˆ๋‹ค.

 

 

Spread syntax (...) - JavaScript | MDN

Spread syntax (...) allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where ze

developer.mozilla.org

  • Spread Syntax (...) allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

 

์†”์งํžˆ ๋ญ”๋ง์ธ์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๋Œ€์ถฉ ๋ณต์ˆ˜์˜ ์—˜๋ฆฌ๋จผํŠธ๋“ค์„ ํ‘œํ˜„ํ•  ๋•Œ ์“ฐ์ธ๋‹ค๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// expected output: 6

 

์˜ˆ์ œ๋ฅผ ๋ด๋„ ์ธ์ˆ˜๋กœ numbers๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ๊ณผ ๋ฌด์Šจ์ฐจ์ด์ธ์ง€ ์•Œ์ˆ˜๊ฐ€ ์—†๋‹ค.

 

ํ•ด์„ค์„ ๋ณด๋‹ˆ spread syntax์˜ ํŠน์ง•์€ ์ด๋ ‡๋‹ค๊ณ  ํ•œ๋‹ค.

 

  • the real value in spread syntax is that it works with the same value, no matter how many elements are contained in object, array. etc.

์š”๋Š” ์ „๋‹ฌํ•˜๋Š” ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด ๋“ฑ์— ๋‹ด๊ฒจ์žˆ๋Š” ์š”์†Œ ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€๋Š” ์ƒ๊ด€์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

์œ„ ์˜ˆ์‹œ์—์„œ ๋ฐฐ์—ด์— ๋‹ด๊ธด ๊ฐ’์„ [1,2,3,4] ๋กœ ๋Š˜๋ ค๋„ sum์—๋Š” 1,2,3๋งŒ ์ „๋‹ฌ๋˜์–ด ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๊ฒŒ 6์ด ๋‚˜์˜จ๋‹ค.

 

  • It is commonly used when you want to add a new item to a local data store, or display all stored items plus a new addition.

ํ”ํžˆ ๋กœ์ปฌ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ์— ์ƒˆ๋กœ์šด ์•„์ดํ…œ์„ ์ €์žฅํ•  ๋•Œ ์“ฐ์ด๊ฑฐ๋‚˜ ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€ํ•œ ํ›„ ์ €์žฅ๋œ ์•„์ดํ…œ์„ ํ‘œ์‹œํ•  ๋•Œ ์“ฐ์ธ๋‹ค๊ณ  ํ•œ๋‹ค.

 

let numberStore = [0, 1, 2];
let newNumber = 12;
numberStore = [...numberStore, newNumber];

 

์œ„ ์ฝ”๋“œ๋ฅผ ์ฝ˜์†”์—์„œ ์‹คํ–‰ํ•ด๋ณด๋ฉด

 

 

numberStore์— ๋‘ ๋ฐฐ์—ด์ด ํ•ฉ์ณ์ง„ ๋ฐฐ์—ด์ด ํ• ๋‹น๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์œ„์™€ ๊ฐ™์ด ์ƒˆ๋กœ์šด ๊ฐ’์„ ๊ณ„์†ํ•ด์„œ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์งค ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

 


๐Ÿ“ฃ Syntax

 

//For function calls: 
// pass all elements of iterableObj as arguments to function myFunction
myFunction(...iterableObj);

// For array literals or strings: 
// combine two arrays by inserting all elements from iterableObj
[...iterableObj, '4', 'five', 6]; 

// For object literals (new in ECMAScript 2018):
// pass all key:value pairs from an object
let objClone = {...obj};

 


๐Ÿ“ฃ Spread in array literals

 

spread syntax๋Š” ํŠนํžˆ ๋ฐฐ์—ด์—์„œ ์œ„๋ ฅ์„ ๋ฐœํœ˜ํ•œ๋‹ค.

 

push๋‚˜ splice๊ฐ™์€ API์—†์ด๋„ ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

let parts = ['shoulders', 'knees'];
let lyrics = ['head', ...parts, 'and', 'toes'];
//  ["head", "shoulders", "knees", "and", "toes"]

 

 

์Šคํ”„๋ ˆ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

let arr = [1, 2, 3];
let arr2 = [...arr]; // like arr.slice()

arr2.push(4);
//  arr2 becomes [1, 2, 3, 4]
//  arr remains unaffected

 

๋˜ concat()์„ ๋Œ€์ฒด๊ฐ€๋Šฅํ•˜๋‹ค.

 

let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];

//  Append all items from arr2 onto arr1
arr1 = arr1.concat(arr2);

 

์ด ์ฝ”๋“œ๋ฅผ ์Šคํ”„๋ ˆ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด

 

let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];

arr1 = [...arr1, ...arr2];
//  arr1 is now [0, 1, 2, 3, 4, 5]
// Note: Not to use const otherwise, it will give TypeError (invalid assignment)

 

์ด๋ ‡๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค. 

 

ํ™•์‹คํžˆ ๋ฐฐ์—ด์— ์ƒˆ๋กœ์šด ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” ์Šคํ”„๋ ˆ๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ์‰ฝ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด API์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ ์žฅ๋‹จ์ ์€ ๋ฌด์—‡์ธ์ง€๋„ ๊ถ๊ธˆํ•ด์ง€๋Š”๋ฐ ์ผ๋‹จ ์˜ค๋Š˜์€ ์—ฌ๊ธฐ๊นŒ์ง€๋งŒ..

 

 

 

๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ์„ ๋ณด๋‹ˆ ์ต์Šคํ”Œ๋กœ๋Ÿฌ๋งŒ ์ฃฝ์–ด์žˆ๋Š” ๋ชจ์Šต.. ์–ด์„œ ๋นจ๋ฆฌ ์—ญ์‚ฌ์†์œผ๋กœ ์‚ฌ๋ผ์ง€๊ธธ

๋ฐ˜์‘ํ˜•

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ

๋Œ“๊ธ€ ์˜์—ญ