๐Ÿ“šย Reference


Rest Parameters


function f(a, b, ...theArgs) {
  // ...
}
// ๊ฐ์ฒด
const purpleCuteSlime = {
   name: "์Šฌ๋ผ์ž„",
   attribute: "cute",
   color: "purple",
 };

const { color, ...cuteSlime } = purpleCuteSlime;
console.log(color);
console.log(cuteSlime);

const { attribute, ...slime } = cuteSlime;
console.log(attribute);
console.log(slime);

// ๋ฐฐ์—ด
const numbers = [0, 1, 2, 3, 4, 5, 6];
const [one, ...rest] = numbers;
console.log(one);
console.log(rest);

// ํ•จ์ˆ˜
function sum1(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 result1 = sum1(1, 2, 3, 4, 5, 6);
console.log(result1);

console.log("---------------------------");

function sum2(...rest) {
    return rest.reduce((acc, current) => acc + current, 0);
}

const result2 = sum2(1, 2, 3, 4, 5, 6);
console.log(result2); // 21