๐Ÿ“’Javascript ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•, Rest ํŒŒ๋ผ๋ฏธํ„ฐ :)

zooyahoยท2022๋…„ 4์›” 9์ผ
0

study with me

๋ชฉ๋ก ๋ณด๊ธฐ
9/19
post-thumbnail

1. ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•(Spread Syntax)

  • ...์€ ํ•˜๋‚˜๋กœ ๋ญ‰์ณ์ ธ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ฐ’๋“ค์˜ ์ง‘ํ•ฉ์„ ํŽผ์ณ์„œ ๊ฐœ๋ณ„์ ์ธ ๊ฐ’๋“ค์˜ ๋ชฉ๋ก์œผ๋กœ ๋งŒ๋“ฌ.
  • Array, String, Map, Set, DOM์ปฌ๋ ‰์…˜(NodeList, HTMLCollection), arguments์™€ ๊ฐ™์ด for of๋ฌธ์œผ๋กœ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์— ํ•œ์ •๋จ.

๐Ÿ“Œ ์Šคํ”„๋ ˆ๋“œ์˜ ๊ฒฐ๊ณผ๋ฌผ์€ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ , ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ ๊ฐ’์˜ ๋ชฉ๋ก์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ๋งฅ์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿ”ต ํ•จ์ˆ˜ ํ˜ธ์ถœ์˜ ์ธ์ˆ˜ ๋ชฉ๋ก

๐Ÿ‘พ #1 Math.Max๋ฉ”์„œ๋“œ์—์„œ ์‚ฌ์šฉ

  • Math.Max๋ฉ”์„œ๋“œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ •ํ•  ์ˆ˜ ์—†๋Š” ๊ฐ€๋ณ€ ์ธ์ž ํ•จ์ˆ˜์ด๋‹ค.
  • ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์•„ ์ธ์ˆ˜ ์ค‘ ์ตœ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const arr = [2,5,1];
console.log(Math.Max(arr)); // NaN

console.log(Math.Max(...arr)) // 5

๐Ÿ‘พ #2 RestํŒŒ๋ผ๋ฏธํ„ฐ์™€์˜ ์ฐจ์ด์ 

function foo(...rest){
  console.log(rest); // 1,2,3 -> [1,2,3]
}
foo(...[1,2,3]); // [1,2,3] -> 1,2,3

๐Ÿ”ต ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

๐Ÿ“Ž concat

  • ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด๋งŒ์œผ๋กœ 2๊ฐœ์˜ ๋ฐฐ์—ด์„ 1๊ฐœ์˜ ๋ฐฐ์—ด๋กœ ๊ฒฐํ•ฉ ๊ฐ€๋Šฅํ•จ.
// ES5
var arr = [1,2].concat([3,4]);
console.log(arr); // [1,2,3,4]

//ES6
const arr = [...[1,2], ...[3,4]];
console.log(arr); // [1,2,3,4]

๐Ÿ“Ž splice

๐Ÿ‘พ #1

// ES5
var arr1 = [1,4];
var arr2 = [2,3];

// ๊ธฐ๋Œ€ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์•„๋‹˜
arr1.splice(1,0,arr2);
console.log(arr1); // [1,[2,3],4]

๐Ÿ‘พ #2

  • apply๋ฉ”์„œ๋“œ์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜(๋ฐฐ์—ด)์€ apply๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜์— ํ•ด์ฑ„๋˜์–ด ์ „๋‹ฌ๋จ~!
var arr1 = [1,4];
var arr2 = [2,3];

Array.prototype.splice.apply(arr1, [1,0].concat(arr2));
console.log(arr1); // [1,2,3,4]

๐Ÿ‘พ #3 ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ• ์‚ฌ์šฉ

// ES6
const arr1 = [1,4];
const arr2 = [2,3];

arr1.splice(1,0, ...arr2);
console.log(arr1); // [1,2,3,4]

๐Ÿ“Ž ๋ฐฐ์—ด ๋ณต์‚ฌ

  • ์Šคํ”„๋ ˆ๋“œ๋ฌธ๋ฒ•์œผ๋กœ ๋ฐฐ์—ด ๋ณต์‚ฌ ์‹œ ์–•์€ ๋ณต์‚ฌ๊ฐ€ ๋œ๋‹ค.
// ES5
var origin = [1,2];
var copy = origin.slice();

console.log(copy); // [1,2]
console.log(copy === origin); //false

//ES6
const origin = [1,2];
const copy = [...origin];

console.log(copy); // [1,2]
console.log(copy === origin); //false

๐Ÿ”ต ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

  • ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•์˜ ๋Œ€์ƒ์€ ์ดํ„ฐ๋Ÿฌ๋ธ”์ด์–ด์•ผ ํ•˜์ง€๋งŒ ์Šคํ”„๋ ˆ๋“œ ํ”„๋กœํผํ‹ฐ ์ œ์•ˆ์€ ์ผ๋ฐ˜ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•์˜ ์‚ฌ์šฉ์„ ํ—ˆ๊ฐ€ํ•จ!!!

๐Ÿ‘พ #1

const obj = { x:1, y:2 };
const copy = { ...obj };
console.log( obj === copy ); // false

// ๊ฐ์ฒด ๋ณ‘ํ•ฉ
const merged = { x:1, y:2, ...{ a:3, b:4 } };
console.log(merged); // { x:1, y:2, a:3, b:4 }

๐Ÿ‘พ #2

  • ES6์—์„œ ๋„์ž…๋œ Object.assign๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฐ์ฒด ๋ณ‘ํ•ฉ, ํŠน์ • ํ”„๋กœํผํ‹ฐ ๋ณ€๊ฒฝ ๋˜๋Š” ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
const merged = Object.assign({}, {x:1,y:2}, {y:10, z:3});
console.log(merged); // {x:1, y:10, z:3}

๐Ÿ‘พ #3

  • ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•์œผ๋กœ Object.assign๋ฉ”์„œ๋“œ๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Œ.
// ๊ฐ์ฒด ๋ณ‘ํ•ฉ
const merged = { ...{x:1,y:2}, ...{y:10, z:3} };
console.log(merged); // {x:1, y:10, z:3}

// ํŠน์ • ํ”„๋กœํผํ‹ฐ ๋ณ€๊ฒฝ
const changed = { ...{x:1,y:2}, y:5 }; // == ...{y:5}
console.log(changed); // {x:1, y:5}

// ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€
const added = { ...{x:1,y:2}, z:5 }; // == ...{z:5}
console.log(added); // {x:1, y:2, z:5}

2. RestํŒŒ๋ผ๋ฏธํ„ฐ

  • ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„ ์•ž์— ...์„ ๋ถ™์—ฌ์„œ ์ •์˜ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์˜๋ฏธ
  • ํ•จ์ˆ˜์— ์ „๋‹ฌ๋œ ์ธ์ˆ˜๋“ค์˜ ๋ชฉ๋ก์„ ๋ฐฐ์—ด๋กœ ์ „๋‹ฌ๋ฐ›์Œ.
  • ํ• ๋‹น๋œ ์ธ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ธ์ˆ˜๋“ค์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜, ๋ฐ˜๋“œ์‹œ ๋งˆ์ง€๋ง‰ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์ž‘์„ฑํ•ด์•ผํ•จ.
  • ๋‹จ ํ•œ๋ฒˆ๋งŒ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Œ.
  • ํ•จ์ˆ˜ ์ •์˜ ์‹œ ์„ ์–ธํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•จ์ˆ˜ ๊ฐ์ฒด์˜ lengthํ”„๋กœํผํ‹ฐ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Œ.
function foo(param1, pram2, ...rest) {
  // ์ธ์ˆ˜๋“ค์˜ ๋ชฉ๋ก์„ ๋ฐฐ์—ด๋กœ ์ „๋‹ฌ๋ฐ›์Œ
  console.log(...rest); // [ 3,4,5 ]
}

foo(1,2,3,4,5)
console.log(foo.length) // 2

RestํŒŒ๋ผ๋ฏธํ„ฐ์™€ arguments๊ฐ์ฒด

  • ES5์—์„œ๋Š” ๊ฐ€๋ณ€ ์ธ์ž ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌ ๋ฐ›๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ arguments ๊ฐ์ฒด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌ ๋ฐ›์Œ.

๐Ÿ“Ž arguments๊ฐ์ฒด

  • ๐Ÿ‘พ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ „๋‹ฌ๋œ ์ธ์ˆ˜๋“ค์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ์œ ์‚ฌ๋ฐฐ์—ด ๊ฐ์ฒด์ด๋ฉฐ, ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ง€์—ญ ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.
function sum() {
  console.log(arguments); 
}
sum(1,2); // {length:2, '0':1, '1':2}
  • ๐Ÿ‘พ arguments๊ฐ์ฒด๋Š” ๋ฐฐ์—ด์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Function.prototype.call ์ด๋‚˜ Function.prototype.apply๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•จ.
function sum() {
  var array = Array.prototype.slice.call(arguments);
  
  return array.reduce(function (pre, cur){
    return pre + cur;
  }, 0);
}

console.log(sum(1,2,3,4,5)) // 15

๐Ÿ“Ž Rest ํŒŒ๋ผ๋ฏธํ„ฐ

  • ๐Ÿ‘พ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜ ์ž์ฒด์˜ arguments ๊ฐ์ฒด๋ฅผ ๊ฐ–์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ rest ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ.
function sum(...args) {
  return args.reduce((pre, cur) => pre + cur, 0);
}

console.log(sum(1,2,3,4,5)) // 15
profile
์ฆ๊ฒ๊ฒŒ ๊ฐœ๋ฐœํ•˜์ž ์ฅฌ์•ผํ˜ธ๐Ÿ‘ป

0๊ฐœ์˜ ๋Œ“๊ธ€