์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- float
- position
- REACT
- CSS
- ๋ฐ๋ธ์ฝ์ค
- ์๊ณ ๋ฆฌ์ฆ
- ์ฝ๋ฉํ ์คํธ
- history api
- useEffect
- fetch API
- ๋ธ๋ก๊ทธ
- ๋ฐ๋ธ์ฝ์ค3๊ธฐ
- ํ๋ก ํธ์๋
- Gatsby
- ํ๋ก๊ทธ๋๋จธ์ค
- useRef
- Props
- Flex
- ์๋ฐ์คํฌ๋ฆฝํธ
- Today
- Total
Daehyunii's Dev-blog
20์ฅ strict mode ๋ณธ๋ฌธ
20์ฅ strict mode
Daehyunii 2022. 7. 16. 20:2820.1 strict mode๋?
์ฐ์ strict mode๋ฅผ ์๊ธฐ์ํด์๋ ์๋ฌต์ ์ ์ญ์ด๋ผ๋ ๊ฐ๋ ์ ์์์ผ ํ ํ์๊ฐ ์๋ค. ์๋ฌต์ ์ ์ญ์ด๋ ๋ณ์๋ก ์ ์ธํ์ง ์์ ์๋ณ์์ ๊ฐ์ ํ ๋นํ๋ ๊ฒฝ์ฐ ReferenceError๊ฐ ๋ฐ์ํ์ง ์๊ณ ๋ง์น ์ ์ญ ๋ณ์์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค. ๊ทธ ์ด์ ๋ ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ์๋ฌต์ ์ผ๋ก ๋ณ์๋ก ์ ์ธ๋์ง ์์ ์๋ณ์๋ฅผ ์ ์ญ๊ฐ์ฒด์ ํ๋กํผํฐ๋ก ๋์ ์์ฑํ๊ธฐ ๋๋ฌธ์ด๋ค. ์๋ฅผ๋ค์ด window ์ ์ญ๊ฐ์ฒด์ ๋ํ ํํ์ ์๋ต์ด ๊ฐ๋ฅํ๋ฏ๋ก ์ ์ญ๊ฐ์ฒด์ ํ๋กํผํฐ ๋์ ์์ฑ์ด ๋๋ ๊ฒ์ด๋ค.
function foo(){
x = 10; // ์๋ฌต์ ์ ์ญ์ด ์์ฑ๋จ(์ ์ญ ๊ฐ์ฒด window์ ํ๋กํผํฐ๊ฐ ๋๋ค.)
}
foo();
console.log(x); // 10
๊ฐ๋ฐ์์ ์๋์๋ ์๊ด์์ด ๋ฐ์ํ ์๋ฌต์ ์ ์ญ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๋ ์์ธ์ด ๋ ๊ฐ๋ฅ์ฑ์ด ํฌ๋ค. ๋ฐ๋ผ์ ๋ฐ๋์ ๋ณ์ ์ ์ธ ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ณ์๋ฅผ ์ ์ธํ ๋ค์ ์ฌ์ฉํด์ผ ํ๋ค. ํ์ง๋ง ์ฝ๋ ์์ฑ๋ ๊ฒฐ๊ตญ ์ฌ๋์ด ํ๋ ์ผ์ด๊ธฐ์ ์ธ์ ๋ ์ค์๋ ๋ฐ์ํ๋ค. ์ด๋ฅผ ์ง์ํ๊ธฐ ์ํด strict mode๊ฐ ์ถ๊ฐ๋์๋ค. ์ฐธ๊ณ ๋ก ES6์์ ๋์ ๋ ํด๋์ค์ ๋ชจ๋์ ๊ธฐ๋ณธ์ ์ผ๋ก strict mode๊ฐ ์ ์ฉ๋๋ค.
20.2 strcit mode์ ์ ์ฉ
strict mode๋ฅผ ์ ์ฉํ๋ ค๋ฉด ์ ์ญ์ ์ ๋ ๋๋ ํจ์ ๋ชธ์ฒด์ ์ ๋์ 'use strict'; ๋ฅผ ์ถ๊ฐํ๋ค. ์ ์ญ์ ์ ๋์ ์ถ๊ฐํ๋ฉด ์คํฌ๋ฆฝํธ ์ ์ฒด์ strict mode๊ฐ ์ ์ฉ๋๋ค
'use strict';
function foo(){
x = 10;
}
foo();
console.log(x); // ReferenceError (strict mode์์๋ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.)
ํจ์ ๋ชธ์ฒด์ ์ ๋์ ์ถ๊ฐํ๋ฉด ํด๋น ํจ์์ ์ค์ฒฉ ํจ์์ strict mode๊ฐ ์ ์ฉ๋๋ค.
function foo(){
'use strict';
x = 10;
}
foo();
console.log(x); // ReferenceError
์ฃผ์ํด์ผ ํ ์ ์ ์ฝ๋์ ์ ๋์ 'use strict';๋ฅผ ์์น์ํค์ง ์์ผ๋ฉด strict mode๊ฐ ์ ๋๋ก ๋์ํ์ง ์๋๋ค.
function foo(){
x = 10;
'use strict';
}
foo();
console.log(x); // 10
20.3 ์ ์ญ์ strict mode๋ฅผ ์ ์ฉํ๋ ๊ฒ์ ํผํ์.
์ ์ญ์ ์ ์ฉํ strict mode๋ ์คํฌ๋ฆฝํธ ๋จ์๋ก ์ ์ฉ๋๋ค. ์คํฌ๋ฆฝํธ ๋จ์๋ก ์ ์ฉ๋ strict mode๋ ๋ค๋ฅธ ์คํฌ๋ฆฝํธ์ ์ํฅ์ ์ฃผ์ง ์๊ณ ํด๋น ์คํฌ๋ฆฝํธ์ ํ์ ๋์ด ์ ์ฉ๋๋ค. ํ์ง๋ง strict mode ์คํฌ๋ฆฝํธ์ non-strict mode ์คํฌ๋ฆฝํธ๋ฅผ ํผ์ฉํ๋ ๊ฒ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํฌ ์ ์์ผ๋ฏ๋ก ์ ์ญ์ strict mode๋ฅผ ์ ์ฉํ๋ ๊ฒ์ ๋ฐ๋์งํ์ง ์๋ค.
20.4 ํจ์ ๋จ์๋ก strict mode๋ฅผ ์ ์ฉํ๋ ๊ฒ๋ ํผํ์.
strict mode๋ ํจ์ ๋จ์๋ก ์ ์ฉํ ์๋ ์๋ค. ๊ทธ๋ฌ๋ ์ด๋ค ํจ์๋ strict mode๋ฅผ ์ ์ฉํ๊ณ ์ด๋ค ํจ์๋ strict mode๋ฅผ ์ ์ฉํ์ง ์๋ ๊ฒ์ ๋ฐ๋์งํ์ง ์์ผ๋ฉฐ ๋ชจ๋ ํจ์์ ์ผ์ผ์ด strict mode๋ฅผ ์ ์ฉํ๋ ๊ฒ์ ๋ฒ๊ฑฐ๋ก์ด ์ผ์ด๋ค. ๋ฐ๋ผ์ strict mode๋ ์ฆ์ ์คํ ํจ์๋ก ๊ฐ์ผ ์คํฌ๋ฆฝํธ ๋จ์๋ก ์ ์ฉํ๋ ๊ฒ์ด ๋ฐ๋์งํ๋ค.
(function (){
// non-strict mode
var num = 10; // ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ค.
function foo(){
'use strict';
num = 20; // SyntaxError
}
foo();
}());
20.5 strict mode๊ฐ ๋ฐ์์ํค๋ ์๋ฌ
20.5.1 ์๋ฌต์ ์ ์ญ
์ ์ธํ์ง ์์ ๋ณ์๋ฅผ ์ฐธ์กฐํ๋ฉด ReferenceError๊ฐ ๋ฐ์ํ๋ค.
(function (){
'use strict';
num = 20;
console.log(num); // ReferenceError ๋ฐ์
}());
20.5.2 ๋ณ์, ํจ์, ๋งค๊ฐ๋ณ์์ ์ญ์
delete ์ฐ์ฐ์๋ก ๋ณ์, ํจ์, ๋งค๊ฐ๋ณ์๋ฅผ ์ญ์ ํ๋ฉด SyntaxError๊ฐ ๋ฐ์ํ๋ค.(delete ์ฐ์ฐ์๋ ํ๋กํผํฐ ์ญ์ ์ฐ์ฐ์๋ค.)
'use strict';
var i = 100;
delete i;
console.log(i); // SyntaxError ๋ฐ์
20.5.3 ํจ์์ ๋งค๊ฐ๋ณ์ ์ด๋ฆ์ ์ค๋ณต
์ค๋ณต๋ ๋งค๊ฐ๋ณ์ ์ด๋ฆ์ ์ฌ์ฉํ๋ฉด SyntaxError๊ฐ ๋ฐ์ํ๋ค.
(function () {
'use strict';
function foo(x,x){ // Syntax Error ๋ฐ์
return x + x;
}
console.log(foo(1,2));
}());
20.5.4 with ๋ฌธ์ ์ฌ์ฉ
with ๋ฌธ์ ์ฌ์ฉํ๋ฉด SyntaxError๊ฐ ๋ฐ์ํ๋ค. with ๋ฌธ์ ์ ๋ฌ๋ ๊ฐ์ฒด๋ฅผ ์ค์ฝํ ์ฒด์ธ์ ์ถ๊ฐํ๋ค. with ๋ฌธ์ ๋์ผํ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ๋ฐ๋ณตํด์ ์ฌ์ฉํ ๋ ๊ฐ์ฒด ์ด๋ฆ์ ์๋ตํ ์ ์์ด์ ์ฝ๋๊ฐ ๊ฐ๋จํด์ง๋ ํจ๊ณผ๊ฐ ์์ง๋ง ์ฑ๋ฅ๊ณผ ๊ฐ๋ ์ฑ์ด ๋๋น ์ง๋ ๋ฌธ์ ๊ฐ ์์ด with ๋ฌธ์ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ด ์ข๋ค.
'๐ Language & CS knowledge > JavaScript (๋ชจ๋์๋ฐ์คํฌ๋ฆฝํธ Deep Dive)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
22์ฅ this (0) | 2022.07.19 |
---|---|
21์ฅ ๋นํธ์ธ ๊ฐ์ฒด (0) | 2022.07.18 |
19์ฅ ํ๋กํ ํ์ (0) | 2022.07.13 |
18์ฅ ํจ์์ ์ผ๊ธ ๊ฐ์ฒด (0) | 2022.07.12 |
17์ฅ ์์ฑ์ ํจ์์ ์ํ ๊ฐ์ฒด ์์ฑ (0) | 2022.07.10 |