ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JavaScript/VanillaJS] ์กฐ์šฉํ•œ ์•Œ๋žŒ(silence alarm) ์›น ์•ฑ ๋งŒ๋“ค๊ธฐ - ๋ฒˆ์™ธํŽธ(feat. ES6)
    javascript 2019. 4. 28. 23:18

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

     

    ์กฐ์šฉํ•œ ์•Œ๋žŒ(silence alarm) ์›น ์•ฑ ๋งŒ๋“ค๊ธฐ๊ฐ€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์™œ ์›น ์•ฑ ๋งŒ๋“ค๊ธฐ๋ผ๋Š” ์ œ๋ชฉ์œผ๋กœ ๋ฒˆ์™ธ๋ฅผ ์ ๋ƒ? 5ํŽธ ํฌ์ŠคํŒ…์„ ๋ฐœํ–‰ํ•œ ํ›„ ์†Œ์Šค๋ฅผ ์ฒœ์ฒœํžˆ ๋ณด๋‹ˆ ๋งˆ์Œ์— ๋“ค์ง€ ์•Š๋Š” ๋ถ€๋ถ„์ด ์žˆ์–ด์„œ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •์„ ํ–ˆ๊ณ , ์ˆ˜์ •ํ•œ ๋ถ€๋ถ„์„ ๊ณต์œ ํ•˜๊ณ  ์‹ถ์–ด ์ด๋ ‡๊ฒŒ ๋ฒˆ์™ธ ํŽธ์œผ๋กœ ํฌ์ŠคํŒ…์„ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

     

    ์–ด๋Š ๋ถ€๋ถ„์ธ์ง€ ์ฝ”๋“œ๋ฅผ ๋จผ์ € ๋ณด๊ณ  ์ฝ”๋“œ ๋ธ”๋ก ์ดํ›„์— ์„ค๋ช…์„ ์ ๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    alarm.js
    
    const alarmContainer = document.querySelector('.js-alarm');
    const currentTime = alarmContainer.querySelector('h1');
    const setTime = alarmContainer.querySelector('input');
    
    // ์ถ”๊ฐ€ํ•œ ์ฝ”๋“œ
    function currentDate(){
      const date = new Date();
      const hours = date.getHours();
      const minutes = date.getMinutes();
      const seconds = date.getSeconds();
      
      /* ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด */
      return {hours, minutes, seconds};
    }
    
    function getAlarm(){
      const setValue = setTime.value;
      
      /* ES6 ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง(destructuring, ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น) */
      const {hours, minutes, seconds} = currentDate();
      
      const current = `${hours < 10 ? `0${hours}` : hours}:${minutes < 10 ? `0${minutes}` : minutes}`;
    	
      if(current === setValue){
    	window.open('popup.html');
    	clearInterval(alarm);	
      }
    }
    
    function getTime(){
      /* ES6 ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง(destructuring, ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น) */
      const {hours, minutes, seconds} = currentDate();
    
      currentTime.innerText = `${hours < 10 ? `0${hours}` : hours}:${minutes < 10 ? `0${minutes}` : minutes}:${seconds < 10 ? `0${seconds}` : seconds}`;
    }
    
    function init(){
      getTime();
      setInterval(getTime, 1000);
      alarm = setInterval(getAlarm, 1000);
    }
    
    init();
    

     

    ์šฐ๋ฆฌ์˜ ๋จธ๋ฆฟ์†์— ์žˆ๋Š” ๊ธฐ์กด ์†Œ์Šค์™€ ๋น„๊ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (์‘?)

    ๊ฐ ํ•จ์ˆ˜๋งˆ๋‹ค Date ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šค ํ•˜์—ฌ date.getHours, date.getMinutes ๋“ฑ ์‹œ, ๋ถ„, ์ดˆ๋ฅผ ๋ถˆ๋Ÿฌ์™”์—ˆ์ฃ . ์ค‘์š”ํ•œ ๊ฑด ๊ฐ ํ•จ์ˆ˜๋งˆ๋‹ค ๋˜‘๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์žˆ์—ˆ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์ž๋Š” ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒŒ ์˜๋ฌด์ด๋ฉฐ, ๋ฏธ๋•์ด๋ผ๊ณ  ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค.

     

    ์–ด๋–ป๊ฒŒ ์ค‘๋ณต์„ ์—†์•ด๋ƒ?

    function์—์„œ return ํ•  ๋•Œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ES6์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด class๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ setInterval( )๋กœ ๋งค ์ดˆ๋งˆ๋‹ค class๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ธ์Šคํ„ด์Šคํ™” ํ•œ๋‹ค๋Š” ๊ฒŒ ๊บผ๋ฆผ์น™ํ–ˆ๊ธฐ์— function์—์„œ ๊ฐ์ฒด๋ฅผ return ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

     

    ์ด return ๋œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ES6 ๊ธฐ๋Šฅ์˜ ํ•ต์‹ฌ์ธ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง(!), ๋ฐ”๋กœ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์˜คํ•ด์˜ ์†Œ์ง€๊ฐ€ ์žˆ๊ธฐ์— ์กฐ๊ธˆ ๋” ์ •๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ € ํ˜•ํƒœ๋งŒ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์—๋Š” ๋‹ค์–‘ํ•œ ํ˜•ํƒœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    spread ์—ฐ์‚ฐ์ž, rest ์—ฐ์‚ฐ์ž๋„ ์‚ฌ์šฉํ•˜๊ณ , ๊ฐ์ฒด, ๋ฐฐ์—ด ๋“ฑ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ด€๋ จ ์„œ์ ์ด๋‚˜ ์ž๋ฃŒ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ผผ๊ผผํžˆ ์ฝ์–ด๋ณด์…”์•ผ ํ•˜๊ณ , ๋ธ”๋กœ๊ทธ๋ฅผ ์•„์‹ ๋‹ค๋ฉด ์ž์ฃผ ๋“ค์–ด๊ฐ€ ํ•™์Šต ๋ฐ ์ˆ™์ง€๋ฅผ ํ•˜์…”์•ผ ํ•˜๊ณ , ๋ฐฐ์šธ ๋งค๊ฐœ์ฒด๊ฐ€ ์—†๋‹ค๋ฉด MDN(๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น - MDN)์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค. (์ €๋Š” MDN ์˜ˆ์ฐฌ๋ก ์ž์ด๋ฉฐ, vanilla ์‹ ๋ด‰์ž์ž…๋‹ˆ๋‹ค :D)

     

     

    ์œ„์— ์†Œ์Šค๋ฅผ ์ผ๋ถ€ ๋ฐœ์ทŒํ•˜์—ฌ ๊ฐ„๋žตํžˆ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์†Œ์Šค๋ฅผ ์ž ์‹œ ๋ณด์‹ค๊นŒ์š”?

    
    function currentDate(){
      ...(์ƒ๋žต)
      return {hours, minutes, seconds};
    }
    
    // ๊ฐ ํ•จ์ˆ˜ ์•ˆ์— ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”๋œ Local Variable์ž…๋‹ˆ๋‹ค.(ํ•จ์ˆ˜ scope์ž„์„ ์•ˆ๋‚ด๋“œ๋ฆฝ๋‹ˆ๋‹ค.)
    // ํŽธ์˜์ƒ ํ•จ์ˆ˜๋“ค์„ ์ ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.
    const {hours, minutes, seconds} = currentDate();
    

     

    currentDate()๋Š” {hours, minutes, seconds}๋ฅผ return ํ•˜๊ณ  ์žˆ๊ธฐ์— ์•„๋ž˜ ์†Œ์Šค๊ณผ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋˜๊ณ ,

    
    const {hours, minutes, seconds} = {hours, minutes, seconds};
    

     

    return ๋œ ๊ฐ์ฒด๋Š” ๋ณธ์ธ ์ž๋ฆฌ์— ์ผ์น˜ํ•œ ํ‚ค์— ๋Œ€์ž…๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์•„๋ž˜ ์†Œ์Šค์™€ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    
    const hours = hours, minutes = minutes, seconds = seconds;
    

     

    ํ˜น์‹œ๋‚˜ ์ œ๊ฐ€ ์„ค๋ช…ํ•œ ๊ฒƒ์— ์ž˜๋ชป๋œ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ๊ฐ€์ฐจ์—†์ด ์ง€์ ํ•ด์ฃผ์„ธ์š”.

    ํ‹€๋ฆฐ ๋ถ€๋ถ„์€ ๋น ๋ฅด๊ฒŒ ํ•™์Šตํ•˜๊ณ  ๊ธ€ ์ˆ˜์ •ํ•˜์—ฌ ์ž˜๋ชป๋œ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์ผ์ด ์—†๋„๋ก ์ฃผ์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    ์ด๋ ‡๊ฒŒ ES6๋ฅผ ๋˜ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ง›์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค :)

    ์•ž์œผ๋กœ์˜ ๊ณ„ํš์„ ์กฐ๊ธˆ ์ ์–ด๋‘๊ณ  ์‹ถ์–ด์„œ ๋ช‡ ์ž ์ ์Šต๋‹ˆ๋‹ค.

     

    Vanilla JS๋กœ ๊ฐ„๋‹จํ•œ ์›น ์•ฑ์„ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค ๊ณ„ํš์ด ์žˆ๊ธด ํ•˜๋‚˜, ๊ทธ ์ „์— ES6 ํ•™์Šต์„ ์šฐ์„ ์œผ๋กœ ํ•˜์—ฌ, ์ถ”ํ›„ ๊ฐ„๋‹จํ•œ ์›น ์•ฑ์„ ๋งŒ๋“ค ๋•Œ ํ•™์Šตํ•œ ES6๋ฅผ ๊ฝค ์ ์šฉํ•˜์—ฌ ์ €์˜ ํฌ์ŠคํŒ…์„ ๋ด์ฃผ์‹œ๋Š” ๋ถ„๋“ค์—๊ฒŒ ์žฌ๋ฐŒ๊ณ  ์•Œ์ฐฌ ๋‚ด์šฉ์„ ๋“œ๋ฆฌ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

     

    ๊ทธ๋Ÿผ ๋‹น๋ถ„๊ฐ„์€ vanilla js ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์•„๋‹Œ ES6 ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ ๋ต™๊ฒ ์Šต๋‹ˆ๋‹ค.

    ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! XD

     

     

    ๋Œ“๊ธ€

Posted by Bejerry.