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

JS_๊ธฐ๋ณธ ๋ฌธ๋ฒ• (30)_ Spread Operator

by KASSID 2023. 8. 6.

๋ชฉ์ฐจ

    728x90

    JS์˜ Sperad Operator์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

     

    0. Spread Operator

    Spread Operator๋Š” ์ ์ด 3๊ฐœ(...)์ธ ์—ฐ์‚ฐ์ž์ด๋‹ค.

    ์ด ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ์—ฌ๋Ÿฌ ๊ณณ์— ํ™œ์šฉ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ๋จผ์ € ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด์„œ ์—ฐ์‚ฐ์ž์˜ ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋ฉด

     

    ์˜ˆ์‹œ

    let a = [1, 2, 3];
    let b = "Hello";
    
    console.log(...a);
    console.log(...b);

     

     

    ์ด์ฒ˜๋Ÿผ  array ํ˜น์€ ๋ฌธ์ž์—ด ๊ฐ™์€ ๊ฒƒ๋“ค์„ ํ•˜๋‚˜์”ฉ ๋ถ„ํ•ดํ•ด์ค€๋‹ค.

     

     


    ํ™œ์šฉ 1. Array ๋ณ‘ํ•ฉ & ๋ณต์‚ฌ

    ์ฒซ๋ฒˆ์งธ ํ™œ์šฉ์€ array๋ฅผ ํ•ฉ์น˜๊ฑฐ๋‚˜ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

     

    1) ๋ณ‘ํ•ฉ

    let a = [1, 2];
    let b = [3, 4];
    
    let c = [...a, ...b];
    console.log(c);
    c = [...b, ...a];
    console.log(c);

     

     

    ์ด์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ array๋ฅผ ์†์‰ฝ๊ฒŒ ํ•ฉ์น  ์ˆ˜ ์žˆ๋‹ค.

     

     

    2) ๋ณต์‚ฌ(๊นŠ์€๋ณต์‚ฌ)

    ๋ฐฐ์—ด์„ ๊นŠ์€ ๋ณต์‚ฌ(๊ฐ’๋งŒ์„ ๋ณต์‚ฌ)ํ•˜๊ณ  ์‹ถ์„ ๋•Œ์—๋„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    let d = ['a', 'b'];
    
    let e = [...d];
    console.log(e);
    
    e[0] = 'A';
    console.log(d);
    console.log(e);

     

     

    ์ด์ฒ˜๋Ÿผ ๊นŠ์€ ๋ณต์‚ฌ๊ฐ€ ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ์–•์€ ๋ณต์‚ฌ ์˜ˆ์‹œโ–ผ

     

     

     


    ํ™œ์šฉ 2. Object ๋ณ‘ํ•ฉ & ๋ณต์‚ฌ

    ๋‘๋ฒˆ์งธ ํ™œ์šฉ์€ object๋ฅผ ํ•ฉ์น˜๊ณ  ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

     

    1) ๋ณ‘ํ•ฉ

    let ob1 = {name: "Kassid"};
    let ob2 = {...ob1, age: 25};
    
    console.log(ob2);

     

     

    ์ด์ฒ˜๋Ÿผ ๋ณ‘ํ•ฉ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

    ์ด๋•Œ ๋งŒ์•ฝ key๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์–ด๋–จ๊นŒ?

    let ob1 = {name: "Kassid"};
    let ob2 = {...ob1, name: "Blue"};
    let ob3 = {name: "Blue", ...ob1};
    
    console.log(ob2);
    console.log(ob3);

     

     

    Key ๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ, ๋งˆ์ง€๋ง‰์œผ๋กœ ์ •์˜๋œ ๊ฐ’์œผ๋กœ ์ •ํ•ด์ง„๋‹ค.

     

     

    2) ๋ณต์‚ฌ

    let ob1 = {name: "Kassid"};
    let ob2 = {...ob1};
    
    console.log(ob2);

     

     

     


    ํ™œ์šฉ3. ํ•จ์ˆ˜

    ์„ธ๋ฒˆ์งธ๋Š” ํ•จ์ˆ˜์—์„œ์˜ ํ™œ์šฉ์ด๋‹ค.

     

    1) ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— array ๋ถ„ํ•ดํ•˜์—ฌ ์ „๋‹ฌ

    array์˜ ์š”์†Œ๋“ค์„ ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ• ๋•Œ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ 

    Spread  Operator๋กœ ๋ถ„ํ•ดํ•˜์—ฌ ์†์‰ฝ๊ฒŒ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

    function func(a, b, c){
      console.log(a + b + c);
    }
    
    let arr = [1, 2, 3];
    
    func(...arr);

     

     

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

    rest ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ์•ž์— Spread Operator๋ฅผ ๋ถ™์ธ ํ˜•ํƒœ์ด๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’๋“ค์„ array๋กœ ๋ฌถ์–ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    function func(...parm){
      console.log(parm);
    }
    
    func(1, 2, 3);

     

     

    rest ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ฃผ์˜ํ•  ์ ์€ ํ•ญ์ƒ ๋งˆ์ง€๋ง‰์— ์œ„์น˜ํ•ด์•ผํ•œ๋‹ค๋Š” ์ ์ด๋‹ค!

     

     

     


    โ˜†์ฐธ๊ณ  apply, call

    JS์˜ ๋‚ด์žฅ ํ•จ์ˆ˜์ธ apply์™€ call์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

     

     

    ์‚ฌ์šฉ๋ฒ•

    ์‹คํ–‰ํ• ํ•จ์ˆ˜.apply(์ ์šฉ์œ„์น˜, ํŒŒ๋ผ๋ฏธํ„ฐ);
    
    ์‹คํ–‰ํ• ํ•จ์ˆ˜.call(์ ์šฉ์œ„์น˜, ํŒŒ๋ผ๋ฏธํ„ฐ);

     

     

    ์˜ˆ์‹œ

    let infoTemp = {
      sayHi: function(){console.log("Hello " + this.name)}
    }
    
    let info = {
      name: "Kassid"
    }
    
    infoTemp.sayHi.apply(info);
    infoTemp.sayHi.call(info);

     

     

    ์ด๋ ‡๊ฒŒ ํ˜„์žฌ ๊ฐ์ฒด์— ์—†๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    (apply, call์˜ ๊ธฐ๋Šฅ์€ ๊ฐ™๋‹ค.)

     

    ์ด๋•Œ apply, call์˜ ์ฐจ์ด์ ์ด ์žˆ๋‹ค๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌํ• ๋•Œ์ด๋‹ค.

     

    apply๋Š” array์˜ ํ˜•ํƒœ๋กœ ์ „๋‹ฌํ•˜๋ฉด๋˜๊ณ , (ex [1, 2, ...] )

    call์€ ํ•˜๋‚˜์”ฉ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค. (ex 1, 2, ... )

     

     

    ์ด ํ•จ์ˆ˜๋“ค์€ spread operator๊ฐ€ ์—†์„ ๋•Œ ๊ทธ ์—ญํ• ์„ ๋Œ€์‹ ํ•˜์˜€๋Š”๋ฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์˜€๋‹ค.

    let arr = [1, 2, 3];
    
    func(...arr); //spread operator ์‚ฌ์šฉ
    func.apply(undefinded, arr); //apply ์‚ฌ์šฉ

     

    ๋Œ“๊ธ€