20์žฅ-strict mode

#20์žฅ

20.1 strict mode๋ž€?

  • ์˜คํƒ€๋‚˜ ๋ฌธ๋ฒ• ์ง€์‹์˜ ๋ฏธ๋น„๋กœ ์ธํ•œ ์‹ค์ˆ˜๋Š” ์–ธ์ œ๋‚˜ ๋ฐœ์ƒํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, ์˜ค๋ฅ˜๋ฅผ ์ค„์—ฌ ์•ˆ์ •์ ์ธ ์ฝ”๋“œ๋ฅผ ์ƒ์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ข€ ๋” ๊ทผ๋ณธ์ ์ธ ์ ‘๊ทผ์ด ํ•„์š”ํ•˜๋‹ค.

    • ๋‹ค์‹œ ๋งํ•ด, ์ž ์žฌ์ ์ธ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ์–ด๋ ค์šด ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ณ  ๊ทธ ํ™˜๊ฒฝ์—์„œ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ์ด ์ข€ ๋” ๊ทผ๋ณธ์ ์ธ ํ•ด๊ฒฐ์ฑ…์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

    • ์ด๋ฅผ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ES5๋ถ€ํ„ฐ strict mode๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์ด๋‹ค.

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

  • ESLint ๊ฐ™์€ ๋ฆฐํŠธ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•ด๋„ strict mode์™€ ์œ ์‚ฌํ•œ ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

20.2 strict mode์˜ ์ ์šฉ

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

  • ์ „์—ญ์˜ ์„ ๋‘์— ์ถ”๊ฐ€ํ•˜๋ฉด ์Šคํฌ๋ฆฝํŠธ ์ „์ฒด์— strict mode๊ฐ€ ์ ์šฉ๋œ๋‹ค.

  • ํ•จ์ˆ˜ ๋ชธ์ฒด ์„ ๋‘์— ์ถ”๊ฐ€ํ•˜๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜์™€ ์ค‘์ฒฉํ•จ์ˆ˜์— strict mode๊ฐ€ ์ ์šฉ๋œ๋‹ค.

  • ์ฝ”๋“œ ์„ ๋‘์— โ€˜use strictโ€™;๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์œผ๋ฉด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

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

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

    <script>
    	'use strict';
    </script>
    <script>
    	x = 1; //์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
    </script>
    <script>
    	'use strict';
    	y = 1; //Refernce Error
    </script>
    
  • strict mode ์Šคํฌ๋ฆฝํŠธ์™€ non-script mode ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ˜ผ์šฉํ•˜๋Š” ๊ฒƒ์€ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ํŠนํžˆ ์™ธ๋ถ€ ์„œ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ non-strict mode์ธ ๊ฒฝ์šฐ๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ „์—ญ์— strict mode๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š๋‹ค.

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

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

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

  • ํ•จ์ˆ˜ ๋‹จ์œ„๋กœ๋„ 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 indentifier in strict mode.

  function foo(a) {
    delete a; //SyntaxError: Delete of an unqualified indentifier in strict mode.
  }
  delete foo; //SyntaxError: Delete of an unqualified indentifier 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๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

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

(function () {
	'use strict';

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

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

20.6.2 arguments ๊ฐ์ฒด

  • arguments ๊ฐ์ฒด๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋„˜๊ธด ์ธ์ž๋“ค์ด ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค.

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

(function (a) {
  "use strict";
  a = 2;
  console.log(arguments);
})(1);

Last updated