ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JavaScript/ES6] Arrow(ํ™”์‚ดํ‘œ) ํ•จ์ˆ˜ - ๊ตฌ๋ฌธํŽธ
    javascript 2019. 5. 10. 18:34

    ์•ˆ๋…•ํ•˜์„ธ์š”, Jerry์ž…๋‹ˆ๋‹ค.

     

    ์ด๋ฒˆ ํฌ์ŠคํŒ…์€ arrow function(ํ™”์‚ดํ‘œ ํ•จ์ˆ˜)์— ๋Œ€ํ•ด์„œ ์ž‘์„ฑํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. arrow function(ํ™”์‚ดํ‘œ ํ•จ์ˆ˜)์˜ ๊ฒฝ์šฐ ์–ธ๊ธ‰ํ•  ๊ฒŒ ๋งŽ์•„์„œ 2ํŽธ ํ˜น์€ 3ํŽธ์œผ๋กœ ๋‚˜๋ˆ ์„œ ํฌ์ŠคํŒ…ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

     

    ECMAScript 6(์ €์ž, ๊น€์˜๋ณด) ์ฑ…์˜ ๋‚ด์šฉ๊ณผ MDN์˜ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

     

    ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ๊ธฐ์กด์˜ ํ•จ์ˆ˜ ํ˜•ํƒœ์ธ function(parameter) { code } ํ˜•ํƒœ๋ฅผ ์ถ•์•ฝํ•œ ๊ฒƒ์œผ๋กœ ๊ธฐ์กด function ํ‘œํ˜„๋ณด๋‹ค ์งง์Šต๋‹ˆ๋‹ค.

    ์•„๋ž˜ ์ž‘์„ฑ๋œ ๊ตฌ๋ฌธ์„ ํ•œ ๋ฒˆ ๋ณผ๊นŒ์š”?

     

    ๊ธฐ๋ณธ ๊ตฌ๋ฌธ
    ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•˜๋‚˜๋ฟ์ธ ๊ฒฝ์šฐ ๊ด„ํ˜ธ๋Š” ์„ ํƒ์‚ฌํ•ญ
    (param) => { code };
    param => { code };
    ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜๋Š” ๊ด„ํ˜ธ๊ฐ€ ํ•„์š”
    ( ) => { code };
    ์—ฌ๋Ÿฌ ํŒŒ๋ผ๋ฏธํ„ฐ(param)๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Œ
    (param1, param2, ..., paramN) => { code };

    (param1, param2, ..., paramN) => expression;
    // ๋‹ค์Œ๊ณผ ๋™์ผ => { return expression };

     

    ๊ณ ๊ธ‰ ๊ตฌ๋ฌธ
    ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํ‘œํ˜„( { } )์„ ๋ฐ˜ํ™˜(return) ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•จ์ˆ˜์˜ ๋ณธ๋ฌธ์„ ๊ด„ํ˜ธ( ( ) ) ์†์— ๋„ฃ์Œ
    param => ( {key: value} );
    Rest ํŒŒ๋ผ๋ฏธํ„ฐ, Default ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ง€์›
    (param1, param2, ...rest) => { code };
    (param1 = defaultValue1, param2, ..., paramN = defaultValueN) => { code };
    ํŒŒ๋ผ๋ฏธํ„ฐ ๋ชฉ๋ก ๋‚ด์— ๋น„๊ตฌ์กฐํ™”(Destructuring, ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น)๋„ ์ง€์›
    ([one, two]) = [1, 2]) => one + two;
    ({key: sum} = {key: 10 + 20}) => { code };

    let f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c;
    f(); // 6

     

    MDN์—์„œ๋Š” ๊ธฐ๋ณธ ๊ตฌ๋ฌธ๊ณผ ๊ณ ๊ธ‰ ๊ตฌ๋ฌธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์„ค๋ช…์„ ํ•ฉ๋‹ˆ๋‹ค. ์ €๋„ ๋‚˜๋ˆ„์–ด์„œ ํฌ์ŠคํŒ…ํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” ์ œ๊ฐ€ ์•„์ง Rest ํŒŒ๋ผ๋ฏธํ„ฐ์™€ Default ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํฌ์ŠคํŒ…ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„  ๊ธฐ๋ณธ ๊ตฌ๋ฌธ์— ๋Œ€ํ•ด์„œ๋งŒ ํฌ์ŠคํŒ…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    ์„ค๋ช… (ํ•จ์ˆ˜ ์ž‘์„ฑ๋ฐฉ๋ฒ•)

    ์œ„์— ์ž‘์„ฑํ•œ ๊ตฌ๋ฌธ์„ ์ฐธ๊ณ ํ•˜๋ฉฐ arrow function(์ดํ•˜, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜)์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค :)

     

    ์šฐ์„  ์ฒ˜์Œ์—๋„ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š”

    
    function(param)  { code }
    

    ์œ„์™€ ๊ฐ™์€ ๊ธฐ์กด์˜ function ํ˜•ํƒœ๋ฅผ ์ถ•์•ฝํ•ด์„œ

    
    (param) => { code }
    

    ์œ„์™€ ๊ฐ™์ด ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. function ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„์š”. ๋Œ€์‹  ํ™”์‚ดํ‘œ( => )๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

     

    ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜ ๋ณด๊ณ  ๊ฐˆ๊นŒ์š”?

    ES5
    
    // ES5-1
    var es5 = function(one, two) {
      return one + two;
    };
    
    var sum = es5(1, 2);
    console.log(sum);
    // ๊ฒฐ๊ณผ๋Š” 3 ์ž…๋‹ˆ๋‹ค.
    
    // ES5-2
    function _es5(one, two) {
      return one + two;
    }
    
    var _sum = _es5(1, 2);
    console.log(_sum);
    // ๊ฒฐ๊ณผ๋Š” 3 ์ž…๋‹ˆ๋‹ค.
    

     

    ES6
    
    // ES6-1
    let es6 = (one, two) => {
      return one + two;
    };
    
    let result = es6(1, 2);
    console.log(result);
    // ๊ฒฐ๊ณผ๋Š” 3 ์ž…๋‹ˆ๋‹ค.
    
    // ES6-2
    let _es6 = (one, two) => one + two;
    
    let _result = _es6(1, 2);
    console.log(_result);
    // ๊ฒฐ๊ณผ๋Š” 3 ์ž…๋‹ˆ๋‹ค.
    

     

    ES5๋Š” ๊ธฐ์กด function ํ‘œํ˜„์ด๊ณ , ES6๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ํ‘œํ˜„์ž…๋‹ˆ๋‹ค. ์œ„์— ์ž‘์„ฑํ•œ ๊ธฐ๋ณธ ๊ตฌ๋ฌธ์„ ๋ณด์‹œ๋ฉด ์ค‘๊ด„ํ˜ธ( {} )๋ฅผ ์ƒ๋žตํ–ˆ์„ ๋•Œ return ํ‚ค์›Œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ตฌ๋ฌธ์„ ์˜ˆ์‹œ ์ฝ”๋“œ ์ฃผ์„ ES6-2์— ์ž‘์„ฑํ•ด๋ดค์Šต๋‹ˆ๋‹ค.

     

    ์—”์ง„์ด ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋‚˜๊ฒŒ ๋˜๋ฉด ๋นŒํŠธ์ธ Function ์˜ค๋ธŒ์ ํŠธ์˜ prototype์— ์—ฐ๊ฒฐ๋œ ๋ฉ”์„œ๋“œ๋กœ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ es6 ๋ณ€์ˆ˜(ํ˜น์€ _es6 ๋ณ€์ˆ˜)์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ๋ฌด๋ช…/์ต๋ช…(anonymous) ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด์„  ์œ„ ์˜ˆ์‹œ ์ฝ”๋“œ์™€ ๊ฐ™์ด ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

    ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ ์ž์‹ ์˜ this, arguments, super ๋˜๋Š” new.target์„ ๋ฐ”์ธ๋”ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ด€๋ จ ์ฝ˜ํ…์ธ ์˜ ์–‘์ด ๋งŽ์„ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋ผ์„œ ํฌ์ŠคํŒ…์„ ๋‚˜๋ˆ„๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    ๋‹ค์Œ ํฌ์ŠคํŒ…๊นŒ์ง€ ์ข‹์€ ๋‚˜๋‚ ๋“ค ๋ณด๋‚ด์„ธ์š” :)

    ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค, JERRY ์˜€์Šต๋‹ˆ๋‹ค!

     

     

    ๋Œ“๊ธ€

Posted by Bejerry.