Home 20์žฅ, strict mode
Post
์ทจ์†Œ

20์žฅ, strict mode

๐Ÿ”– 20์žฅ, strict mode

๐Ÿ“Œ ๋ฐฐ์šด ๋‚ด์šฉ ๋ฐ ๊ธฐ์–ตํ•˜๊ณ  ์‹ถ์€ ๋‚ด์šฉ

strict mode

1
2
3
4
5
6
function foo() {
  x = 10;
}
foo();

console.log(x); // ?

์ด ์ฝ”๋“œ์—์„œ console.log(x)์˜ ๊ฒฐ๊ณผ๊ฐ’์€ 10์ด๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์Šค์ฝ”ํ”„ ์ฒด์ธ์„ ํ†ตํ•ด ์‹๋ณ„์ž๋ฅผ ๊ฒ€์ƒ‰์„ ํ•˜๊ธฐ ์‹œ์ž‘ํ•˜๋Š”๋ฐ ์ƒ์œ„ ์Šค์ฝ”ํ”„์— x๊ฐ€ ์—†์–ด x๋ฅผ ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์•”๋ฌต์  ์ „์—ญ์„ ์„ ์–ธํ•œ๋‹ค.

์ฐธ๊ณ ๋กœ ์ฐธ์กฐ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

  • ์•ˆ์ •์ ์ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ์–ด๋ ค์šด ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ธฐ์œ„ํ•ด ES5๋ถ€ํ„ฐ strict mode๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.
  • strict mode(์—„๊ฒฉ ๋ชจ๋“œ): ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์˜ ๋ฌธ๋ฒ•์„ ์ข€๋” ์—„๊ฒฉํžˆ ์ ์šฉํ•˜์—ฌ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๊ฑฐ๋‚˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์˜ ์ตœ์ ํ™” ์ž‘์—…์— ๋ฌธ์ œ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ์— ๋Œ€ํ•ด ๋ช…์‹œ์ ์ธ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๋ชจ๋“œ
  • ESLint๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ๋„ strict mode์™€ ๋™์ผํ•œ ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Œ
    • ESLint: ์ •์  ๋ถ„์„ ๊ธฐ๋Šฅ์„ ํ†ตํ•ด ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ์ „์— ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์Šค์บ”ํ•˜์—ฌ ๋ฌธ๋ฒ•์  ์˜ค๋ฅ˜๋งŒ์ด ์•„๋‹ˆ๋ผ ์ž ์žฌ์  ์˜ค๋ฅ˜๊นŒ์ง€ ์ฐพ์•„๋‚ด๊ณ  ์˜ค๋ฅ˜์˜ ์›์ธ์„ ๋ฆฌํฌํŒ…ํ•ด์ฃผ๋Š” ์œ ์šฉํ•œ ๋„๊ตฌ
    • ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜์„ ์„ฑ์ • ํŒŒ์ผ ํ˜•ํƒœ๋กœ ์ •์˜ํ•˜๊ณ  ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ์Œ
  • ํด๋ž˜์Šค์™€ ๋ชจ๋“ˆ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ strict ๋ชจ๋“œ๊ฐ€ ์ ์šฉ๋จ

strict mode์˜ ์ ์šฉ

  • ์ „์—ญ์˜ ์„ ๋‘ ๋˜๋Š” ํ•จ์ˆ˜ ๋ชธ์ฒด์˜ ์„ ๋‘์— 'use strict';๋ฅผ ์ถ”๊ฐ€

    • 1
      2
      3
      4
      5
      6
      
      'use strict';
          
      function foo() {
        x = 10; // ReferenceError: x is not defined
      }
      foo();
      
    • 1
      2
      3
      4
      5
      6
      
      function foo() {
        'use strict';
          
        x = 10; // ReferenceError: x is not defined
      }
      foo();
      
  • ์ฝ”๋“œ์˜ ์„ ๋‘์— โ€˜use strict';๋ฅผ ์œ„์น˜์‹œํ‚ค์ง€ ์•Š์œผ๋ฉด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์Œ

์ „์—ญ์— strict mode๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ”ผํ•˜์ž

  • ์ „์—ญ์— ์ ์šฉํ•œ strict mode๋Š” ์Šคํฌ๋ฆฝํŠธ ๋‹จ์œ„๋กœ ์ ์šฉ๋จ

  • ์Šคํฌ๋ฆฝํŠธ ๋‹จ์œ„๋กœ ์ ์šฉ๋œ strict mode๋Š” ๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ์— ์˜ํ–ฅ์„ ์ฃผ์ง€์•Š๊ณ  ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ์— ํ•œ์ •๋˜์–ด ์ ์šฉ๋จ

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      
      <!DOCTYPE html>
      <html>
      <body>
        <script>
          'use strict';
        </script>
        <script>
          x = 1; // ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
          console.log(x); // 1
        </script>
        <script>
          'use strict';
          
          y = 1; // ReferenceError: y is not defined
          console.log(y);
        </script>
      </body>
      </html>
      
    • strict ์Šคํฌ๋ฆฝํŠธ์™€ non-strict ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ˜ผ์šฉํ•˜๋Š” ๊ฒƒ์€ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ์ˆ˜ ์žˆ์Œ

      • ์™ธ๋ถ€ ์„œ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ non-strict mode์ธ ๊ฒฝ์šฐ๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ „์—ญ strict mode๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ถŒ์žฅํ•˜์ง€ ์•Š์Œ

      • ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜๋กœ ์Šคํฌ๋ฆฝํŠธ ์ „์ฒด๋ฅผ ๊ฐ์‹ธ ์Šค์ฝ”ํ”„๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ณ  ์ฆ‰์‹œ์‹คํ–‰ ํ•จ์ˆ˜ ์„ ๋‘์— strict mode ์ ์šฉ

      • 1
        2
        3
        4
        5
        6
        
        // ์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜์˜ ์„ ๋‘์— strict mode ์ ์šฉ
        (function () {
          'use strict';
              
          // Do something...
        }());
        

ํ•จ์ˆ˜ ๋‹จ์œ„๋กœ strict mode๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ”ผํ•˜์ž

  • ํ•จ์ˆ˜ ๋‹จ์œ„๋กœ strict mode๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์ง€๋งŒ, ๊ฐ๊ฐ ์ ์šฉํ•˜๊ณ  ์ ์šฉ์•ˆํ•˜๊ณ  ํ•˜๋Š” ๊ฒƒ์€ ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š๊ณ  ๋ชจ๋“  ํ•จ์ˆ˜์— ์ผ์ผํžˆ strict mode๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ฒˆ๊ฑฐ๋กœ์›€

    • strict mode๊ฐ€ ์ ์šฉ๋œ ํ•จ์ˆ˜๋ฅผ ์ฐธ์กฐํ•  ํ•จ์ˆ˜ ์™ธ๋ถ€ ์ปจํ…์ŠคํŠธ์— strict mode๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฌธ์ œ๋ฅผ ์•ผ๊ธฐํ•จ

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      
      (function () {
        // non-strict mode
        var lะตt = 10; // ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
          
        function foo() {
          'use strict';
          
          let = 20; // SyntaxError: Unexpected strict mode reserved word
        }
        foo();
      }());
      
  • strict mode๋Š” ์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜๋กœ ๊ฐ์‹ผ ์Šคํฌ๋ฆฝํŠธ ๋‹จ์œ„๋กœ ์ ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•จ

strict mode๊ฐ€ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์—๋Ÿฌ

์•”๋ฌต์  ์ „์—ญ

  • ์„ ์–ธํ•˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ReferenceError ๋ฐœ์ƒ

    • 1
      2
      3
      4
      5
      6
      
      (function () {
        'use strict';
          
        x = 1;
        console.log(x); // ReferenceError: x is not defined
      }());
      

๋ณ€์ˆ˜, ํ•จ์ˆ˜, ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์‚ญ์ œ

  • delate ์—ฐ์‚ฐ์ž๋กœ ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ญ์ œํ•˜๋ฉด SyntaxError ๋ฐœ์ƒ

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      
      (function () {
        'use strict';
          
        var x = 1;
        delete x;
        // SyntaxError: Delete of an unqualified identifier in strict mode.
          
        function foo(a) {
          delete a;
          // SyntaxError: Delete of an unqualified identifier in strict mode.
        }
        delete foo;
        // SyntaxError: Delete of an unqualified identifier in strict mode.
      }());
      

๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์˜ ์ค‘๋ณต

  • ์ค‘๋ณต๋œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๋ฉด SyntaxError ๋ฐœ์ƒ

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      
      (function () {
        'use strict';
          
        //SyntaxError: Duplicate parameter name not allowed in this context
        function foo(x, x) {
          return x + x;
        }
        console.log(foo(1, 2));
      }());
      

with ๋ฌธ์˜ ์‚ฌ์šฉ

  • with๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด SyntaxError ๋ฐœ์ƒ

    • with ๋ฌธ์€ ์ „๋‹ฌ๋œ ๊ฐ์ฒด๋ฅผ ์Šค์ฝ”ํ”„ ์ฒด์ธ์— ์ถ”๊ฐ€ํ•จ

    • ๋™์ผํ•œ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ์ฒด ์ด๋ฆ„์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์–ด ์ฝ”๋“œ๊ฐ€ ๊ฐ„๋‹จํ•ด์ง€๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์Œ

      • ์„ฑ๋Šฅ๊ณผ ๊ฐ€๋…์„ฑ์ด ๋‚˜๋น ์ง€๋Š” ๋‹จ์ ์ด ์žˆ์–ด ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•จ
    • 1
      2
      3
      4
      5
      6
      7
      8
      
      (function () {
        'use strict';
          
        // SyntaxError: Strict mode code may not include a with statement
        with({ x: 1 }) {
          console.log(x);
        }
      }());
      

strict mode ์ ์šฉ์— ์˜ํ•œ ๋ณ€ํ™”

์ผ๋ฐ˜ ํ•จ์ˆ˜์˜ this

  • strict mode์—์„œ ํ•จ์ˆ˜๋ฅผ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœํ•˜๋ฉด this์— undefined๊ฐ€ ๋ฐ”์ธ๋”ฉ๋จ

    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ

    • ์—๋Ÿฌ๋Š” ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      
      (function () {
        'use strict';
          
        function foo() {
          console.log(this); // undefined
        }
        foo();
          
        function Foo() {
          console.log(this); // Foo
        }
        new Foo();
      }());
      

arguments ๊ฐ์ฒด

  • strict mode์—์„œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ „๋‹ฌ๋œ ์ธ์ˆ˜๋ฅผ ์žฌํ• ๋‹นํ•˜์—ฌ ๋ณ€๊ฒฝํ•ด๋„ arguments ๊ฐ์ฒด์— ๋ฐ˜์˜๋˜์ง€ ์•Š์Œ

    • 1
      2
      3
      4
      5
      6
      7
      8
      
      (function (a) {
        'use strict';
        // ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ „๋‹ฌ๋œ ์ธ์ˆ˜๋ฅผ ์žฌํ• ๋‹นํ•˜์—ฌ ๋ณ€๊ฒฝ
        a = 2;
          
        // ๋ณ€๊ฒฝ๋œ ์ธ์ˆ˜๊ฐ€ arguments ๊ฐ์ฒด์— ๋ฐ˜์˜๋˜์ง€ ์•Š๋Š”๋‹ค.
        console.log(arguments); // { 0: 1, length: 1 }
      }(1));
      

โ—๏ธ ์ฝ์€ ์†Œ๊ฐ

strict mode๋Š” ์—„๊ฒฉ ๋ชจ๋“œ๋กœ ์•ˆ์ •์ ์ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ์–ด๋ ค์šด ํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๋“ฑ์žฅํ•˜์˜€๋‹ค. ์š”์ฆ˜์€ strict ๋Œ€์‹  TypeScript๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€์ฒดํ•˜๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค. ์™ธ๋ถ€ ์„œ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ non-strict mode๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๊ธฐ๋•Œ๋ฌธ์— ์ฆ‰์‹œ์‹คํ–‰ ํ•จ์ˆ˜๋กœ ๋ฌถ์–ด ์‚ฌ์šฉ์„ ํ•ด์•ผํ•œ๋‹ค. ๋˜ํ•œ ESLint๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋Œ€์ฒดํ•˜์—ฌ๋„ ๋ ๊ฒƒ ๊ฐ™์•˜๋‹ค.

โ“ ๊ถ๊ธˆํ•œ ๋‚ด์šฉ์ด๋‚˜ ์ž˜ ์ดํ•ด๋˜์ง€ ์•Š๋Š” ๋‚ด์šฉ

  • ์™ธ๋ถ€ ์„œ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
    • ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ3์ž๋ฅผ ๋œปํ•˜๋Š” ๋‹จ์–ด
      • ์˜ˆ์‹œ1) ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐœ๋ฐœ๊ณผ ๊ฐœ๋ฐœ์ž ์‚ฌ์ด์—์„œ๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ํ”„๋ ˆ์ž„์›Œํฌ ๋“ฑ
      • ์˜ˆ์‹œ2) ์„œ๋น„์Šค์™€ ์‚ฌ์šฉ์ž ์‚ฌ์ด์—์„œ๋Š” ์›น์„œ๋น„์Šค, ์‘์šฉํ”„๋กœ๊ทธ๋žจ, ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ
This post is licensed under CC BY 4.0 by the author.

19์žฅ, ํ”„๋กœํ† ํƒ€์ž…

21์žฅ, ๋นŒํŠธ์ธ ๊ฐ์ฒด