๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/Vanilla JS

JS_๊ธฐ๋ณธ ๋ฌธ๋ฒ• (22)_ ๋งค๊ฐœ๋ณ€์ˆ˜ (๊ธฐ๋ณธ๊ฐ’, arguments, ๋‚˜๋จธ์ง€ ๋ณ€์ˆ˜, ์ด์ƒ์ ์ธ ํ•จ์ˆ˜?)

by KASSID 2023. 3. 12.

๋ชฉ์ฐจ

    728x90

    JS ๋‹ค๋ฃจ๋Š” ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ณด์ž.

     

    1. ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ๋งŽ์ด ๊ฐ’์„ ์ „๋‹ฌํ•œ ๊ฒฝ์šฐ

     

    ์˜ˆ์‹œ 1)

    ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š” ํ•จ์ˆ˜์ด๋‹ค.

    function sum(a, b){
      return a + b;
    }

     

    console.log(
      sum(1, 2),
      sum(1, 2, 3),
      sum(1, 2, 3, 4)
    );
    
    >>> 3 3 3

    ์œ„์˜ ๊ฒฐ๊ณผ์ฒ˜๋Ÿผ JS์—์„œ๋Š” ๋‹ค๋ฅธ ์–ธ์–ด์™€ ๋‹ค๋ฅด๊ฒŒ

    ํ•จ์ˆ˜ ์ •์˜ ์‹œ ์„ค์ •ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ๋งŽ์ด ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ

    ์ˆœ์„œ๋Œ€๋กœ ๊ฐ’์„ ๋ฐ›์€ ํ›„ ๋‚˜๋จธ์ง€๋Š” ๋ฌด์‹œํ•˜๋ฉฐ ์˜ค๋ฅ˜๋ฅผ ์ผ์œผํ‚ค์ง€ ์•Š๊ณ  ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

     

     


    2. ๊ธฐ๋ณธ๊ฐ’

    ๊ธฐ๋ณธ๊ฐ’(default parameter)์€ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฐ’์ด ๋“ค์–ด์˜ค์ง€ ์•Š์•„๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ’์„ ์„ค์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

    function sum(a = 0, b = 0){
      return a + b;
    }
    
    console.log(
    sum()
    );
    
    >>> 0

     

    ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •์€ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋’ค์ชฝ์— ๋ชฐ๋ ค์„œ ์„ค์ •ํ•ด์•ผํ•œ๋‹ค.

     


    3. arguments

    arguments๋Š” ํ•จ์ˆ˜์— ์ „๋‹ฌ๋œ ๊ฐ’๋“ค์— ํ•ด๋‹นํ•˜๋Š” Arrayํ˜•ํƒœ์˜ ๊ฐ์ฒด์ด๋‹ค.

    ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์ „๋‹ฌ๋œ ๋ชจ๋“  ์ธ์ˆ˜๋“ค์„ ๋ฐฐ์—ด์˜ ํ˜•ํƒœ๋กœ ํ˜•์„ฑํ•˜๊ณ  ์žˆ๋‹ค.

     

    ์ฆ‰, ์œ„์—์„œ ์‚ดํŽด๋ดค๋“ฏ ๊ธฐ์กด ์„ค์ •๋œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ˆ˜๋ณด๋‹ค ๋” ๋งŽ์€ ๊ฐ’์„ ์ „๋‹ฌํ•œ๋‹ค๋ฉด

    ํ•จ์ˆ˜ ์—ฐ์‚ฐ์—์„œ๋Š” ๋„˜์นœ ๋ฐ์ดํ„ฐ๋“ค์ด ๋ฌด์‹œ๋œ ์ฑ„ ์—ฐ์‚ฐ์— ๋Œ€ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ

    ์ „๋‹ฌ๋˜์—ˆ๋˜ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋‘ arguments ๊ฐ์ฒด ์•ˆ์— ์กด์žฌํ•œ๋‹ค.

     

    ์ฃผ์˜ํ•  ์ ์€ arguments ๊ฐ์ฒด๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค!

    function์œผ๋กœ ์ •์˜ํ•œ ํ•จ์ˆ˜๊ฐ€ ๊ณ ์ฐจํ•จ์ˆ˜์ผ ๋•Œ์—๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

     

    ์˜ˆ์‹œ 1)

    function sum(a, b){
      console.log(arguments);
      return a + b;
    }
    
    console.log(
      sum(1, 2, 3, 4)
    );
    
    >>> Arguments(4) [1, 2, 3, 4, callee: ƒ, Symbol(Symbol.iterator): ƒ] //arguments
    >>> 3 //ํ•จ์ˆ˜ ์—ฐ์‚ฐ ๊ฐ’

    ์œ„์ฒ˜๋Ÿผ ์ „๋‹ฌํ•œ ๋ชจ๋“  ๊ฐ’๋“ค์ด arguments ์•ˆ์— ์กด์žฌํ•œ๋‹ค.

     

    ํŠน์ง• 1_ ๋ฐฐ์—ด์˜ ํ˜•ํƒœ๋ฅผ ํ•œ ๊ฐ์ฒด์ด๋ฏ€๋กœ ์ธ๋ฑ์‹ฑ ๊ฐ€๋Šฅ

    function sum(a, b){
      console.log(arguments[3]);
      return a + b;
    }
    
    console.log(
      sum(1, 2, 3, 4)
    );
    
    >>> 4 //์ธ๋ฑ์‹ฑ ๊ฐ’
    >>> 3 //ํ•จ์ˆ˜ ์—ฐ์‚ฐ ๊ฐ’

     

    ํŠน์ง• 2_ iterable์ด๋ฏ€๋กœ for ~ of ํ™œ์šฉ ๊ฐ€๋Šฅ

    function sum(){ //๋งค๊ฐœ ๋ณ€์ˆ˜ ์ˆ˜๋ฅผ ์ •ํ•˜์ง€ ์•Š์Œ
      let result = 0;
      for (item of arguments){ //์ž…๋ ฅ๋ฐ›์€ ๊ฐ’๋“ค์˜ ๊ฐ์ฒด ํ™œ์šฉ
        if(typeof item !== 'number') continue; //ํƒ€์ž… ํ™•์ธ
        result += item;
      }
      return result
    }
    
    console.log(
      sum(1, 2, 3, 4),
      sum(5, 10)
    );
    
    >>> 10 15

     

    ํ™œ์šฉ ํ•˜๊ธฐ

    const add = (a, b) => a + b;
    const sub = (a, b) => a - b;
    const mul = (a, b) => a * b;
    const div = (a, b) => a / b;
    
    function combineFunc() {
      return (x, y) => {
        let result = x;
        for (const func of arguments){
          if (typeof func !== 'function') continue; //ํ•จ์ˆ˜๋งŒ ์ „๋‹ฌ๋ฐ›์Œ
          result = func(result, y); //ํ˜„์žฌ ์—ฐ์‚ฐ๊ฐ’๊ณผ y๊ฐ’ ์ „๋‹ฌ
        }
        return result;
      }
    }
    
    const add_mul = combineFunc(add, mul);
    
    // console.log(add_mul); //ํ•จ์ˆ˜ ๋ฐ˜ํ™˜
    console.log(add_mul(3, 2)); // (3 + 2) * 2
     
    >>> 10

     


    4. ๋‚˜๋จธ์ง€ ๋ณ€์ˆ˜ (... ๊ทธ๋ฃน๋ช…)

    ๋‚˜๋จธ์ง€ ๋ณ€์ˆ˜(rest parameters)๋Š” ํŠน์ • ๋งค๊ฐœ๋ณ€์ˆ˜ ๋’ค์— ์ž„์˜ ๊ฐœ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์„ ๋ฐ›์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

    ๊ธฐ๋ณธ๊ฐ’๊ณผ ๊ฐ™์ด ๋งˆ์ง€๋ง‰ ์ธ์ž๋กœ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

    ๋˜ํ•œ ์‹ค์ œ ๋ฐฐ์—ด์ด ๋ฐ˜ํ™˜๋œ๋‹ค.

     

    ์˜ˆ์‹œ 1)

    console.log(
      reminder(3, 12, 'Kassid', '์ฒญ์†Œ', '๊ณผ์ œ')
    );
    
    function reminder(month, day, name, ...doIt){
      console.log();
      let doItList = ''
      for (const item of doIt){
        if (doItList) doItList += ', ';
        doItList += item;
      }
      return `${month}์›” ${day}์ผ Todo List : ${doItList}`
    }

    ๋ฌผ๋ก  ์œ„์˜ ์˜ˆ์‹œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋ฉ”์†Œ๋“œ ์ค‘ ํ•˜๋‚˜์ธ join์„ ํ™œ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋๋‚ผ ์ˆ˜ ์žˆ์ง€๋งŒ
    for ~ of๋ฅผ ํ™œ์šฉํ•ด๋„ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

     

    ์˜ˆ์‹œ 2)

    const add = (a, b) => a + b;
    const sub = (a, b) => a - b;
    const mul = (a, b) => a * b;
    const div = (a, b) => a / b;
    
    function combineFunc(x, y, ...funcs) {
      let result = x;
      for (const func of funcs) {
        if (typeof func !== 'function') continue;
        result = func(result, y);
      }
      return result;
    }
    
    console.log(
      combineFunc(3, 2, add, mul)
    );
     
    >>> 10

    arguments ์˜ˆ์‹œ๋กœ ํ™œ์šฉํ•œ ํ•จ์ˆ˜์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๋„๋ก ๋งŒ๋“ค์–ด ๋ณด์•˜๋‹ค.

     

     

     

     

     

     

     

     

     

     

     


    ๋งค๊ฐœ ๋ณ€์ˆ˜์™€ ์ธ์ˆ˜ ๊ด€์ , ์ด์ƒ์ ์ธ ํ•จ์ˆ˜?

    ํ•จ์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•  ๋•Œ ์ด์ƒ์ ์ธ ํ•จ์ˆ˜๋Š” ์–ด๋–ค ๊ฒƒ์ผ๊นŒ?

    ๋งค๊ฐœ ๋ณ€์ˆ˜์™€ ์ธ์ˆ˜์˜ ๊ด€์ ์—์„œ ๋ฐ”๋ผ๋ณด์ž.

     

    ๋จผ์ €, ์œ„ํ—˜ํ•œ ํ•จ์ˆ˜.

    ์™ธ๋ถ€์˜ ๋ณ€์ˆ˜๋ฅผ ์ง์ ‘ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์€ ์œ„ํ—˜ํ•œ ํ•จ์ˆ˜๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

    let a = 0;
    let b = [1, 2];
    
    function func(x, y){
      x++;
      y[0]++;
    }
    
    func(a, b);
    
    console.log(a);
    console.log(b);
    
    >>> 0 //๋ณ€๊ฒฝ X (์›์‹œ ๋ณ€์ˆ˜) 
    >>> [2, 2] //๋ณ€์ˆ˜ O (์ฐธ์กฐ ๋ณ€์ˆ˜)

     

     

    ๋ฐ˜๋Œ€๋กœ ์ด์ƒ์ ์ธ ํ•จ์ˆ˜!

    ๋ฐ›์€ ๊ฐ’๋“ค์— ๋Œ€ํ•ด์„œ๋งŒ ์ฒ˜๋ฆฌํ•˜๊ณ  ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์ด์ƒ์ ์ธ ํ•จ์ˆ˜๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

    let a = 0;
    let b = [1, 2];
    
    function addOne(x){
      return x+1;
    }
    
    a = addOne(a);
    b[0] = addOne(b[0]);
    
    console.log(a);
    console.log(b);
    
    // ๋ชจ๋‘ ๋ณ€๊ฒฝ
    >>> 1
    >>> [2, 2]

     

    ์ •๋ฆฌ

    ์œ„ํ—˜ํ•œ ํ•จ์ˆ˜ = ์™ธ๋ถ€ ํ™˜๊ฒฝ ๋ณ€๊ฒฝ

    ์ด์ƒ์ ์ธ ํ•จ์ˆ˜ = ๋ฐ›์€ ๊ฐ’๋“ค์— ๋Œ€ํ•ด์„œ๋งŒ ์ฒ˜๋ฆฌ ํ›„, ์ƒˆ ๊ฐ’ ๋ฐ˜ํ™˜

    ๋Œ“๊ธ€