๐Ÿ”’ 20.1 strict mode๋ž€?

  function foo() {
    x = 10;
  }
  foo();

  console.log(x); // ?

์œ„์˜ ์ฝ”๋“œ์—์„œ x๋Š” ์„ ์–ธ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ReferenceError์„ ๋ฐœ์ƒ ์‹œํ‚ฌ๊ฒƒ ๊ฐ™์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์— ์˜ํ•ด ์•”๋ฌต์ ์œผ๋กœ ์ „์—ญ ๊ฐ์ฒด x๊ฐ€ ๋™์  ์ƒ์„ฑ๋˜๋Š”๋ฐ ์ด๋ฅผ ์•”๋ฌต์  ์ „์—ญ์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์•”๋ฌต์  ์ „์—ญ์€ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์›์ธ์ด ๋  ๊ฐ€๋Šฅ์„ฑ์ด ํฌ๊ธฐ ๋•Œ๋ฌธ์— strict mode๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

strict mode๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์˜ ๋ฌธ๋ฒ•์„ ์ข€ ๋” ์—„๊ฒฉํžˆ ์ ์šฉํ•˜์—ฌ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๊ฑฐ๋‚˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์˜ ์ตœ์ ํ™” ์ž‘์—…์— ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ์— ๋Œ€ํ•ด ๋ช…์‹œ์ ์ธ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.


๐Ÿ”’ 20.2 strict mode์˜ ์ ์šฉ

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

  'use strict';

  function foo() {
    x = 10; // ReferenceError: x is not defined
  }
  foo();

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

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

๐Ÿ”’ 20.4 ํ•จ์ˆ˜ ๋‹จ์œ„๋กœ strict mode๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ๋„ ํ”ผํ•˜์ž

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

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

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

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

  (function () {
    'use strict';

    x = 1;
    console.log(x); // ReferenceError: x is not defined
  }());

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

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

  (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.
  }());

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

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

  (function () {
    'use strict';

    //SyntaxError: Duplicate parameter name not allowed in this context
    function foo(x, x) {
      return x + x;
    }
    console.log(foo(1, 2));
  }());

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

with ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด SyntaxError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. with๋ฌธ์€ ์„ฑ๋Šฅ๊ณผ ๊ฐ€๋…์„ฑ์ด ๋‚˜๋น ์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— with๋ฌธ์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๊ฒƒ์ด ์ข‹๋‹ค.

  (function () {
    'use strict';

    // SyntaxError: Strict mode code may not include a with statement
    with({ x: 1 }) {
      console.log(x);
    }
  }());

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

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

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

  (function () {
    'use strict';

    function foo() {
      console.log(this); // undefined
    }
    foo();

    function Foo() {
      console.log(this); // Foo
    }
    new Foo();
  }());

20.6.2 arguments ๊ฐ์ฒด

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

  (function (a) {
    'use strict';
    // ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ „๋‹ฌ๋œ ์ธ์ˆ˜๋ฅผ ์žฌํ• ๋‹นํ•˜์—ฌ ๋ณ€๊ฒฝ
    a = 2;

    // ๋ณ€๊ฒฝ๋œ ์ธ์ˆ˜๊ฐ€ arguments ๊ฐ์ฒด์— ๋ฐ˜์˜๋˜์ง€ ์•Š๋Š”๋‹ค.
    console.log(arguments); // { 0: 1, length: 1 }
  }(1));
profile
์ด์‚ฌ์ค‘์ž…๋‹ˆ๋‹ค!๐ŸŒŸhttps://velog.io/@devkyoung2

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด