
function foo() {
// ์์ง ์ ์ธํ์ง ์์ x์ ๊ฐ 10์ ํ ๋น
x = 10;
}
foo();
console.log(x); // 10
์์ง ์ ์ธํ์ง ์์ ๋ณ์์ธ x์ 10์ด๋ผ๋ ๊ฐ์ ํ ๋นํ๋ค.
์ด๋ค ์ผ์ด ์ผ์ด๋ ๊น?
JS ์์ง์, ๋ณ์ x๊ฐ ์ด๋์์ ์ ์ธ๋์๋์ง ์ฐพ์์ผ ๊ฐ์ ํ ๋นํ ์ ์๋ค.
scope chain์ ํตํด์ ์ ์ธ ์์น๋ฅผ ๊ฒ์ํ๊ธฐ ์์ํ๋ค.
์ผ๋จ foo ํจ์์ scope ์์ ๋ณ์ x์ ์ ์ธ์ ๊ฒ์ํ๋ค.
๊ทธ๋ฌ๋ ์ ์ธ์ด ์๊ธฐ ๋๋ฌธ์ ๊ฒ์์๋ ์คํจํ ๊ฒ์ด๋ค.
๋ค์์ผ๋ก๋ foo ํจ์ ์ปดํ ์คํธ์ ์์ ์ค์ฝํ์์ ๋ณ์ x์ ์ ์ธ์ ๊ฒ์ํ๋ค.
๊ทธ๋ฌ๋ ์์ ๊ทธ ์์์๋ x๋ณ์์ ์ ์ธ์ด ์กด์ฌํ์ง ์๋๋ค.
ReferenveError๋ฅผ ๋ฐ์์์ผ์ผํ ๊ฒ ๊ฐ์ง๋ง, JS ์์ง์ ์๋ฌต์ ์ผ๋ก ์ ์ญ ๊ฐ์ฒด์ x ํ๋กํผํฐ๋ฅผ ๋์ ์์ฑํ๋ค.
์ด๋ ์ ์ญ ๊ฐ์ฒด์ x ํ๋กํผํฐ๋ ๋ง์น ์ ์ญ ๋ณ์์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค.
์ด๋ฐ ํ์์ ์๋ฌต์ ์ ์ญ์ด๋ผ๊ณ ํ๋ค.
์ด๋ํ ๊ฐ๋ฐ์์ ์๋์ ์๊ด์์ด ๋ฐ์ํ๊ณ , ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๋ ์์ธ์ด ๋ ๊ฐ๋ฅ์ฑ์ด ํฌ๋ค.
๋ฐ๋ผ์ var, let, const ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ณ์๋ฅผ ์ ์ธํ ๋ค์์ ์ฌ์ฉํ๋๋ก ํ์.
๊ทธ๋ฐ๋ฐ ์ค์๋ก ์ด๋ฐ ํ์์ด ๋ฐ์ํ๋ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น?
์๋ฌด๋ฆฌ ๊ผผ๊ผผํ๊ฒ ์์
์ ํด๋ ์ฐ๋ฆฌ๋ ์ธ๊ฐ์ด๊ธฐ ๋๋ฌธ์,, ์คํ๋ฑ์ ์ค์๋ก ์ด๋ฐ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ญ ใ
ฃใ
๋ค.
์ด๋ฅผ ์ง์ํ๊ธฐ ์ํด ES5๋ถํฐ๋ strict mode๊ฐ ์ถ๊ฐ๋์๋ค.
strict mode : JS ์ธ์ด์ ๋ฌธ๋ฒ์ ์ข ๋ ์๊ฒฉํ ์ ์ฉํ์ฌ ์ค๋ฅ๋ฅผ ๋ฐ์์ํฌ ๊ฐ๋ฅ์ฑ์ด ๋๊ฑฐ๋ JS ์์ง์ ์ต์ ํ ์์
์ ๋ฌธ์ ๋ฅผ ์ผ์ผํฌ ์ญ ใ
ฃใ
๋ ์ฝ๋์ ๋ํด ๋ช
์์ ์ธ ์๋ฌ๋ฅผ ๋ฐ์์ํค๋ ๋ชจ๋..!
ESLint๊ฐ์ ๋ฆฐํธ ๋๊ตฌ๋ฅผ ์ฌ์ฉํด๋, strict mode์ ์ ์ฌํ ํจ๊ณผ๋ฅผ ์ป์ ์ ์๋ค๊ณ ํ๋ค.
๋ฆฐํธ ๋๊ตฌ๋ ์ ์ ๋ถ์ ๊ธฐ๋ฅ์ ํตํด์ ์์ค ์ฝ๋๋ฅผ ์คํํ๊ธฐ ์ ์ ์์ค์ฝ๋๋ฅผ ์ค์บ ํ์ฌ ๋ฌธ๋ฒ์ ์ค๋ฅ๋ง์ด ์๋๋ผ ์ ์ฌ์ ์ค๋ฅ๊น์ง ์ค๋ฅ์ ์์ธ์ ๋ฆฌํฌํ
ํด์ฃผ๋ ์ ์ฉํ ๋๊ตฌ๋ค.
์ ์ญ์ ์ ๋ ๋๋ ํจ์ ๋ชธ์ฒด์ ์ ๋์ 'use strict;'๋ฅผ ์ถ๊ฐํ๋ค.
์ ์ญ์ ์ ๋์ ์ถ๊ฐํ๋ฉด, ์คํฌ๋ฆฝํธ ์ ์ฒด์ strict mode๊ฐ ์ ์ฉ๋๋ค.
'use strict';
function foo() {
x = 10;
}
foo();
//----------------
function foo() {
'use strict';
x = 10;
}
foo();
//----------------
function foo() {
x = 10;
'use strict'; // 'use strict'๋ฅผ ์ฝ๋ ์ ๋์ ์์น์ํค์ง ์์ผ๋ฉด?
}
foo();
// -> strict mode๊ฐ ์ ๋๋ก ๋์ํ์ง ์๋๋ค.
์ ์ญ์ ์ ์ฉํ strict mode๋ ์คํฌ๋ฆฝํธ ๋จ์๋ก ์ ์ฉ๋๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<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>

์ด๋ ๊ฒ script ๋จ์๋ก ์ ์ฉ๋ strict mode๋, ๋ค๋ฅธ script์ ์ํฅ์ ์ฃผ์ง ์๊ณ ํด๋น ์คํฌ๋ฆฝํธ์ ํ์ ๋์ด ์ ์ฉ๋๋ค.
๊ทธ๋๋ strict mode์ non-strict mode ์คํฌ๋ฆฝํธ๋ฅผ ํผ์ฉํ์ง๋ ๋ง์
-> ์ค๋ฅ๋ฅผ ๋ฐ์์ํฌ์๋ ์์ผ๋...!
// ์ฆ์ ์คํ ํจ์์ ์ ๋์ strict mode ๋ฅผ ์ ์ฉํ๋ ๋ฐฉ๋ฒ
(funciton() {
'use strict';
// Do Something...
}());
strict mode๋ ์ฆ์ ์คํ ํจ์๋ก ๊ฐ์ผ ์คํฌ๋ฆฝํธ ๋จ์๋ก ์ ์ฉํ๋ ๊ฒ์ด ๋ฐ๋์งํ๋ค๊ณ ํฉ๋๋ค
: ์ ์ธํ์ง ์์ ๋ณ์๋ฅผ ์ฐธ์กฐํ๋ฉด ReferenceError๊ฐ ๋ฐ์ํจ
: delete ์ฐ์ฐ์๋ก ๋ณ์, ํจ์ ๋งค๊ฐ๋ณ์๋ฅผ ์ญ์ ํ๋ฉด SyntaxError๊ฐ ๋ฐ์ํจ
: ์ค๋ณต๋ ๋งค๊ฐ๋ณ์ ์ด๋ฆ์ ์ฌ์ฉํ๋ฉด SyntaxError๊ฐ ๋ฐ์ํจ
: with ๋ฌธ์ ์ฌ์ฉํ๋ฉด SyntaxError๊ฐ ๋ฐ์ํจ
with ๋ฌธ์ ์ ๋ฌ๋ ๊ฐ์ฒด๋ฅผ scope chain์ ์ถ๊ฐํ๋ค.
with ๋ฌธ์ ๋์ผํ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ๋ฐ๋ณตํด์ ์ฌ์ฉํ ๋ ๊ฐ์ฒด ์ด๋ฆ์ ์๋ตํ ์ ์์ด์, ์ฝ๋๊ฐ ๊ฐ๋จํด์ง๋ ํจ๊ณผ๊ฐ ์์!
ํ์ง๋ง ์ฑ๋ฅ๊ณผ ๊ฐ๋
์ฑ์ด ๋๋น ์ง๋ ๋ฌธ์ ๊ฐ ์๋ค..
์ด๋ฐ ์ด์ ๋ก,, with ๋ฌธ์ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ด ์ข๋ค๊ณ ํฉ๋๋ค!
strict mode์์ ํจ์๋ฅผ ์ผ๋ฐ ํจ์๋ก์ ํธ์ถํ๋ฉด, this์ undefined๊ฐ ๋ฐ์ธ๋ฉ ๋จ
<- ์์ฑ์ ํจ์๊ฐ ์๋ ์ผ๋ฐ ํจ์ ๋ด๋ถ์์๋, this๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์๊ธฐ ๋๋ฌธ
-> ์๋ฌ๋ ๋ฐ์ํ์ง ์๋๋ค.
(function () {
'use strict';
function foo() {
console.log(this); // undefined
}
foo();
function Foo() {
console.log(this); // Foo {}
}
new Foo();
}());
strict mode์์๋ ๋งค๊ฐ๋ณ์์ ์ ๋ฌ๋ ์ธ์๋ฅผ ์ฌํ ๋นํ์ฌ ๋ณ๊ฒฝํด๋,
arguments ๊ฐ์ฒด์ ๋ฐ์๋์ง ์์
(function (a) {
'use strict';
// ๋งค๊ฐ๋ณ์์ ์ ๋ฌ๋ ์ธ์๋ฅผ ์ฌํ ๋นํ์ฌ ๋ณ๊ฒฝ
a = 2;
// ๋ณ๊ฒฝ๋ ์ธ์๊ฐ arguments ๊ฐ์ฒด์ ๋ฐ์๋์ง ์์
console.log(arguments);
}(1));
