๐Ÿ“‹ ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive | 20์žฅ strict mode

waterglassesยท2022๋…„ 5์›” 7์ผ
0
post-thumbnail

๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive ๋„์„œ์˜ 20์žฅ์„ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

20.1 strict mode๋ž€?

// 20-01
function foo() {
	x = 10;
}

foo();

console.log(x); // ? ReferenceError๊ฐ€ ์•„๋‹Œ ์•”๋ฌต์ ์œผ๋กœ ์ „์—ญ ๊ฐ์ฒด์— x ํ”„๋กœํผํ‹ฐ๋ฅผ ๋™์  ์ƒ์„ฑํ•จ => ์•”๋ฌต์  ์ „์—ญ : ๋”ฐ๋ผ์„œ 10 ์ถœ๋ ฅ

์ž ์žฌ์ ์ธ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ์–ด๋ ค์šด ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ณ  ๊ทธ ํ™˜๊ฒฝ์—์„œ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ์ด ์ข€ ๋” ๊ทผ๋ณธ์ ์ธ ํ•ด๊ฒฐ์ฑ…์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ES5๋ถ€ํ„ฐ strict mode(์—„๊ฒฉ ๋ชจ๋“œ)๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

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

20.2 strict mode์˜ ์ ์šฉ

์ „์—ญ์˜ ์„ ๋‘ ํ˜น์€ ํ•จ์ˆ˜ ๋ชธ์ฒด์˜ ์„ ๋‘์— use strict๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์Šคํฌ๋ฆฝํŠธ ์ „์ฒด์— ์—„๊ฒฉ ๋ชจ๋“œ๊ฐ€ ์ ์šฉ๋œ๋‹ค.

// 20-04
function foo() {
  x = 10; // ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค.
  'use strict';
}
foo();

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

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

20.4 ํ•จ์ˆ˜ ๋‹จ์œ„๋กœ 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๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

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

Ref

  • ์ด์›…๋ชจ ์ €, โŒœ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep DiveโŒŸ, ์œ„ํ‚ค๋ถ์Šค
profile
๋งค ์ˆœ๊ฐ„ ์„ฑ์žฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

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