๐ก 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'๋ฅผ ์์น์ํค์ง ์์ผ๋ฉด 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()
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}

'Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] AJAX & JSON (0) | 2023.01.15 |
---|---|
[FE/JavaScript] ๊ตฌ์กฐ ๋ถํด ํ ๋น(๋์คํธ๋ญ์ฒ๋ง ํ ๋น) (0) | 2022.07.06 |
[FE/JavaScript]this (0) | 2022.06.23 |
[FE/JavaScript] ์์ฑ์ ํจ์์ ์ํ ๊ฐ์ฒด ์์ฑ (0) | 2022.06.13 |
JavaScript - Event (0) | 2022.05.03 |
๋๊ธ