๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Language/JavaScript

[FE/JavaScript] strict mode

by DevIseo 2022. 6. 27.

๐Ÿ’ก strict mode๊ฐ€ ๋ญ”๊ฐ€์š”?

๐Ÿ’กstrict mode๋ฅผ ํ†ตํ•ด ๋ฌด์—‡์„ ์˜ˆ๋ฐฉํ•  ์ˆ˜ ์žˆ์ฃ ?

 

๐ŸŽˆstrict mode๋ž€?

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

ES5(ECMA Script 5)์— ์ถ”๊ฐ€๋œ ํ‚ค์›Œ๋“œ

๐ŸŽˆ strict mode๋ฅผ ํ†ตํ•ด ๋ฌด์—‡์„ ์˜ˆ๋ฐฉํ•  ์ˆ˜ ์žˆ๋‚˜์š”?

use strict๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์˜๋„์น˜ ์•Š์€ ์—ฌ๋Ÿฌ ์ž ์žฌ์ ์ธ ์˜ค๋ฅ˜๋ฅผ ๋ง‰์•„์ฃผ๋Š”๋ฐ ๋งŽ์€ ๋„์›€์„ ์ค€๋‹ค.

์ž˜๋ชป ์‚ฌ์šฉํ•˜๋ฉด ์—†๋˜ ์˜ค๋ฅ˜๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.

๋ฐ˜๋Œ€๋กœ ๊ณต์‹์šฉ์–ด๋Š” ์•„๋‹ˆ์ง€๋งŒ, ๊ฐ€๋” ์—„๊ฒฉํ•˜์ง€ ์•Š์€ ๊ธฐ๋ณธ ๊ฐ’์„ sloppy mode(๋Š์Šจํ•œ ๋ชจ๋“œ) ๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค.

sloppy mode์˜ ์˜ˆ์ œ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์–ด๋–จ๊นŒ?

 function foo(){
		x=10;
}
foo()
console.log(x)  // ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š”?
  • ์ •๋‹ต
    • foo ํ•จ์ˆ˜ ๋‚ด์—์„œ ์„ ์–ธํ•˜์ง€ ์•Š์€ x๋ณ€์ˆ˜์— ๊ฐ’ 10์„ ํ• ๋‹นํ–ˆ๋‹ค.
    • ์ด๋•Œ x ๋ณ€์ˆ˜๋ฅผ ์ฐพ์•„ x์— ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— JS ์—”์ง„์€ x ๋ณ€์ˆ˜๊ฐ€ ์–ด๋””์—์„œ ์„ ์–ธ๋˜์—ˆ๋Š”์ง€ ์Šค์ฝ”ํ”„ ์ฒด์ธ์„ ํ†ตํ•ด ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์‹œ์ž‘!
      • foo ํ•จ์ˆ˜ ์Šค์ฝ”ํ”„โ‡’foo ํ•จ์ˆ˜ ์ปจํ…์ŠคํŠธ์˜ ์ƒ์œ„ ์Šค์ฝ”ํ”„(์œ„์˜ ๊ฒฝ์šฐ ์ „์—ญ ์Šค์ฝ”ํ”„!) ์ˆœ์„œ๋กœ x ๋ณ€์ˆ˜ ์„ ์–ธ ๊ฒ€์ƒ‰
    • ์ตœ์ƒ์œ„ ์Šค์ฝ”ํ”„๊นŒ์ง€ ๊ฒ€์ƒ‰ํ–ˆ์„ ๋•Œ x ๋ณ€์ˆ˜ ์„ ์–ธ์ด ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ReferenceError๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ๊ฒƒ ๊ฐ™๋‹ค.
      • but, ์—„๊ฒฉ๋ชจ๋“œ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์€ ์•”๋ฌต์ ์œผ๋กœ ์ „์—ญ ๊ฐ์ฒด์— x ํ”„๋กœํผํ‹ฐ๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค.์ด๋Ÿฐ ํ˜„์ƒ์„ ์•”๋ฌต์  ์ „์—ญ(implicit global) ์ด๋ผ๊ณ  ํ•œ๋‹ค.์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์›์ธ์ด ๋  ๊ฐ€๋Šฅ์„ฑ์ด ํฌ๋‹ค.</aside>
      • ๋”ฐ๋ผ์„œ ๋ฐ˜๋“œ์‹œ var, let, const ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ ํ›„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
      • <aside> ๐Ÿ’ก ๊ฐœ๋ฐœ์ž์˜ ์˜๋„์™€๋Š” ์ƒ๊ด€์—†์ด ์•”๋ฌต์  ์ „์—ญ์€
      • โ‡’์ „์—ญ ๊ฐ์ฒด์˜ X ํ”„๋กœํผํ‹ฐ๋Š” ๋งˆ์น˜ ์ „์—ญ ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋˜๋Š”๋ฐ

์ด๋Ÿฌํ•œ ์ž ์žฌ์  ์˜ค๋ฅ˜๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด ES5๋ถ€ํ„ฐ strict mode๊ฐ€ ์ถ”๊ฐ€ ๋˜์—ˆ๋‹ค!

+)PLUS

๐Ÿ’ก strict mode ์ ์šฉํ•˜๊ธฐ

๐Ÿ’กstrict mode ์‚ฌ์šฉ ์‹œ ์ฃผ์˜ ์ 

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

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

 

๐Ÿ“ƒstrict mode ์ ์šฉํ•˜๊ธฐ

  • strict mode๋ฅผ ์ ์šฉํ•˜๋ ค๋ฉด ์ „์—ญ์˜ ์„ ๋‘ ๋˜๋Š” ํ•จ์ˆ˜์˜ ๋ชธ์ฒด์˜ ์„ ๋‘์— 'use strict';๋ฅผ ์ถ”๊ฐ€
  • ์ „์—ญ์˜ ์„ ๋‘์— ์ถ”๊ฐ€ํ•˜๋ฉด ์Šคํฌ๋ฆฝํŠธ ์ „์ฒด์— strict mode๊ฐ€ ์ ์šฉ
  • //์ „์—ญ์˜ ์„ ๋‘์— ์ถ”๊ฐ€
    'use strict';
    function foo(){
    	x=10 //ReferenceError: x is not defined
    }
    foo()
    
    
    /////////////////////////////////////////////////////
    
    
    //์ฝ”๋“œ์˜ ์„ ๋‘์— ์ถ”๊ฐ€
    function foo(){
    	'use strict'
    	x=10 //ReferenceError: x is not defined
    }
    foo()
    ๐Ÿšจ ์ฝ”๋“œ์˜ ์„ ๋‘์— 'use strict'๋ฅผ ์œ„์น˜์‹œํ‚ค์ง€ ์•Š์œผ๋ฉด strict mode๊ฐ€ ์ œ๋Œ€๋กœ ๋™์ž‘์•ˆํ•จ
function foo(){
	x=10 //์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š์Œ!
	'use strict'
}
foo()

 

๐Ÿ“ƒstrict mode ์‚ฌ์šฉ ์‹œ ์ฃผ์˜ ์ !

๐Ÿ’ก strict mode๋Š” ์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜๋กœ ๊ฐ์‹ผ ์Šคํฌ๋ฆฝํŠธ ๋‹จ์œ„๋กœ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์ง

 

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

  • ์ „์—ญ์— ์‚ฌ์šฉํ•œ strict mode ๋Š” script ๋‹จ์œ„๋กœ ์ ์šฉ!
  • ํ•˜์ง€๋งŒ strict mode script ์™€ non-sctrict mode script ๋ฅผ ํ˜ผ์šฉํ•˜๋Š” ๊ฒƒ์€ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ!
<!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 mode๋ฅผ ์ ์šฉ
//์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜์˜ ์„ ๋‘์— strict mode ์ ์šฉ
(function () {
	'use strict'
	//Do something...
}());

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

  • ์–ด๋–ค ํ•จ์ˆ˜๋Š” strict mode๋ฅผ ์ ์šฉํ•˜๊ณ  ์–ด๋–ค ํ•จ์ˆ˜๋Š” strict mode๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์€ ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š์Œ.
  • ๋ชจ๋“  ํ•จ์ˆ˜์— ์ผ์ผ์ด strict mode๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๋ฒˆ๊ฑฐ๋กœ์›€
  • ๋˜ํ•œ strict mode๊ฐ€ ์ ์šฉ๋œ ํ•จ์ˆ˜๊ฐ€ ์ฐธ์กฐํ•  ํ•จ์ˆ˜ ์™ธ๋ถ€์˜ ์ปจํ…์ŠคํŠธ์— strict mode๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ๋„ ๋ฌธ์ œ๋ฅผ ๋ฐœ์ƒ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ.
(function (){
	//non-strict mode
	var let = 10 //์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ..
	
	function foo(){
		'use strict';
		let =20 //SyntaxError:Unexpected strict mode reserved word
	}
	foo()
{());

โ‡’ ๋”ฐ๋ผ์„œ strict mode๋Š” ์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜๋กœ ๊ฐ์‹ผ ์Šคํฌ๋ฆฝํŠธ ๋‹จ์œ„๋กœ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•˜๋‹ค!

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

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

์„ ์–ธํ•˜์ง€ ์•Š์€ ๋ณ€์ˆ˜ ์ฐธ์กฐ์‹œ ReferenceError ๋ฐœ์ƒ

(function (){
	'use strict'
	x=1
	console.log(x) //ReferenceError: x is not defined

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

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

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

(function(){
	'use strict'
	//SyntaxError: Delete of an unqualified identifier in strict mode.
	function foo(x,x){
		return x+x
	}
	console.log(foo(1,2))
}())

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

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

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

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

(function(){
	'use strict'
	function foo(){
		console.log(this) //undefined, ๋งŒ์•ฝ strict mode๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด window ์ถœ๋ ฅ!
	}
	foo()
	function Foo(){
		console.log(this) //Foo
	}
	new Foo()
}())

2. arguments ๊ฐ์ฒด

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

why?

strict mode ์—์„œ๋Š” function.arguments์— ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€ํ•ด ๋ณ€๊ฒฝ๋œ ์ธ์ˆ˜๊ฐ€ arguments ๊ฐ์ฒด์— ๋ฐ˜์˜๋˜์ง€ ์•Š๋Š”๋‹ค!

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

	//๋ณ€๊ฒฝ๋œ ์ธ์ˆ˜๊ฐ€ arguments ๊ฐ์ฒด์— ๋ฐ˜์˜๋˜์ง€ ์•Š์Œ
	console.log(arguments) // {0:1, length:1}
}(1))) 

๋งŒ์•ฝ, strict mode๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด? โ†’{0:2, length:1}

๋Œ“๊ธ€