20์ฅ-strict mode
#20์ฅ
20.1 strict mode๋?
์คํ๋ ๋ฌธ๋ฒ ์ง์์ ๋ฏธ๋น๋ก ์ธํ ์ค์๋ ์ธ์ ๋ ๋ฐ์ํ๋ค. ๋ฐ๋ผ์, ์ค๋ฅ๋ฅผ ์ค์ฌ ์์ ์ ์ธ ์ฝ๋๋ฅผ ์์ฐํ๊ธฐ ์ํด์๋ ์ข ๋
๊ทผ๋ณธ์ ์ธ ์ ๊ทผ์ด ํ์ํ๋ค.๋ค์ ๋งํด, ์ ์ฌ์ ์ธ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๊ธฐ ์ด๋ ค์ด ๊ฐ๋ฐ ํ๊ฒฝ์ ๋ง๋ค๊ณ ๊ทธ ํ๊ฒฝ์์ ๊ฐ๋ฐํ๋ ๊ฒ์ด ์ข ๋ ๊ทผ๋ณธ์ ์ธ ํด๊ฒฐ์ฑ ์ด๋ผ๊ณ ํ ์ ์๋ค.
์ด๋ฅผ ์ง์ํ๊ธฐ ์ํด ES5๋ถํฐ
strict mode๊ฐ ์ถ๊ฐ๋ ๊ฒ์ด๋ค.
strict mode๋ ์๋ฐ์คํฌ๋ฆฝํธ ์ธ์ด์ ๋ฌธ๋ฒ์ ์ข ๋ ์๊ฒฉํ ์ ์ฉํ์ฌ ์ค๋ฅ๋ฅผ ๋ฐ์์ํฌ ๊ฐ๋ฅ์ฑ์ด ๋๊ฑฐ๋ ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ์ต์ ํ ์์ ์ ๋ฌธ์ ๋ฅผ ์ผ์ผํฌ ์ ์๋ ์ฝ๋์ ๋ํด
๋ช ์์ ์ธ ์๋ฌ๋ฅผ ๋ฐ์์ํจ๋ค.ESLint ๊ฐ์ ๋ฆฐํธ ๋๊ตฌ๋ฅผ ์ฌ์ฉํด๋ strict mode์ ์ ์ฌํ ํจ๊ณผ๋ฅผ ์ป์ ์ ์๋ค.
20.2 strict mode์ ์ ์ฉ
strict mode๋ฅผ ์ ์ฉํ๋ ค๋ฉด ์ ์ญ์ ์ ๋ ๋๋ ํจ์ ๋ชธ์ฒด์ ์ ๋์ โuse strictโ;๋ฅผ ์ถ๊ฐํ๋ค.
์ ์ญ์ ์ ๋์ ์ถ๊ฐํ๋ฉด ์คํฌ๋ฆฝํธ ์ ์ฒด์ strict mode๊ฐ ์ ์ฉ๋๋ค.
ํจ์ ๋ชธ์ฒด ์ ๋์ ์ถ๊ฐํ๋ฉด ํด๋น ํจ์์ ์ค์ฒฉํจ์์ strict mode๊ฐ ์ ์ฉ๋๋ค.
์ฝ๋ ์ ๋์ โuse strictโ;๋ฅผ ์ถ๊ฐํ์ง ์์ผ๋ฉด ์ ๋๋ก ๋์ํ์ง ์๋๋ค.
20.3 ์ ์ญ์ strict mode๋ฅผ ์ ์ฉํ๋ ๊ฒ์ ํผํ์
์ ์ญ์ ์ ์ฉํ strict mode๋ ์คํฌ๋ฆฝํธ ๋จ์๋ก ์ ์ฉ๋๋ค.
<script> 'use strict'; </script> <script> x = 1; //์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ค. </script> <script> 'use strict'; y = 1; //Refernce Error </script>strict mode ์คํฌ๋ฆฝํธ์ non-script mode ์คํฌ๋ฆฝํธ๋ฅผ ํผ์ฉํ๋ ๊ฒ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํฌ ์ ์๋ค. ํนํ ์ธ๋ถ ์๋ํํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ non-strict mode์ธ ๊ฒฝ์ฐ๋ ์๊ธฐ ๋๋ฌธ์ ์ ์ญ์ strict mode๋ฅผ ์ ์ฉํ๋ ๊ฒ์ ๋ฐ๋์งํ์ง ์๋ค.
์ด๋ฌํ ๊ฒฝ์ฐ ์ฆ์ ์คํ ํจ์๋ก ์คํฌ๋ฆฝํธ ์ ์ฒด๋ฅผ ๊ฐ์ธ์ ์ค์ฝํ๋ฅผ ๊ตฌ๋ถํ๊ณ ์ฆ์ ์คํ ํจ์(์ ์ธํ์๋ง์ ๋ฐ๋ก ํธ์ถ๋๋ ํจ์)์ ์ ๋์ strict mode๋ฅผ ์ ์ฉํ๋ค.
//์ฆ์ ์คํ ํจ์์ ์ ๋์ strict mode ์ ์ฉ (function() { 'use strict'; ... }());
20.4 ํจ์ ๋จ์๋ก strict mode๋ฅผ ์ ์ฉํ๋ ๊ฒ๋ ํผํ์
ํจ์ ๋จ์๋ก๋ strict mode๋ฅผ ์ ์ฉํ ์ ์๋ค.
ํ์ง๋ง, ํจ์๋ง๋ค strict mode๋ฅผ ์ ์ฉํ๋ ์ฌ๋ถ๋ฅผ ๋ค๋ฅด๊ฒ ํ๋ ๊ฒ์ ๋ฐ๋์งํ์ง ์์ผ๋ฉฐ ๋ชจ๋ ํจ์์ ์ผ์ผ์ด ์ ์ฉํ๋ ๊ฒ์ ๋ฒ๊ฑฐ๋ก์ด ์ผ์ด๋ค.
๋ฐ๋ผ์ strict mode๋ ์ฆ์ ์คํ ํจ์๋ก ๊ฐ์ผ ์คํฌ๋ฆฝํธ ๋จ์๋ก ์ ์ฉํ๋ ๊ฒ์ด ๋ฐ๋์งํ๋ค.
20.5 strict mode๊ฐ ๋ฐ์์ํค๋ ์๋ฌ
20.5.1 ์๋ฌต์ ์ ์ญ
์ ์ธํ์ง ์์ ๋ณ์๋ฅผ ์ฐธ์กฐํ๋ฉด ReferenceError๊ฐ ๋ฐ์ํ๋ค
20.5.2 ๋ณ์, ํจ์, ๋งค๊ฐ๋ณ์์ ์ญ์
delete ์ฐ์ฐ์๋ก ๋ณ์, ํจ์, ๋งค๊ฐ๋ณ์๋ฅผ ์ญ์ ํ๋ฉด SyntaxError๊ฐ ๋ฐ์ํ๋ค.
20.5.3 ๋งค๊ฐ๋ณ์ ์ด๋ฆ์ ์ค๋ณต
์ค๋ณต๋ ๋งค๊ฐ๋ณ์ ์ด๋ฆ์ ์ฌ์ฉํ๋ฉด
SyntaxError๊ฐ ๋ฐ์ํ๋ค.
20.5.4 with ๋ฌธ์ ์ฌ์ฉ
with ๋ฌธ์ ์ฌ์ฉํ๋ฉด
SyntaxError๊ฐ ๋ฐ์ํ๋ค. with ๋ฌธ์ ์ ๋ฌ๋ ๊ฐ์ฒด๋ฅผ ์ค์ฝํ ์ฒด์ธ์ ์ถ๊ฐํ๋ค.with๋ฌธ์ ๋์ผํ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ๋ฐ๋ณตํด์ ์ฌ์ฉํ ๋ ๊ฐ์ฒด ์ด๋ฆ์ ์๋ตํ ์ ์์ด์ ์ฝ๋๊ฐ ๊ฐ๋จํด์ง๋ ํจ๊ณผ๊ฐ ์์ง๋ง ์ฑ๋ฅ๊ณผ ๊ฐ๋ ์ฑ์ด ๋๋น ์ง๋ ๋ฌธ์ ๊ฐ ์๋ค.
20.6 strict mode ์ ์ฉ์ ์ํ ๋ณํ
20.6.1 ์ผ๋ฐ ํจ์์ this
strict mode์์ ํจ์๋ฅผ ์ผ๋ฐ ํจ์๋ก์ ํธ์ถํ๋ฉด this์ undefined๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
์์ฑ์ ํจ์๊ฐ ์๋ ์ผ๋ฐ ํจ์ ๋ด๋ถ์์๋ this๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์๊ธฐ ๋๋ฌธ์ด๋ค.
20.6.2 arguments ๊ฐ์ฒด
arguments ๊ฐ์ฒด๋ ํจ์๋ฅผ ํธ์ถํ ๋ ๋๊ธด ์ธ์๋ค์ด ๋ฐฐ์ด ํํ๋ก ์ ์ฅ๋ ๊ฐ์ฒด๋ฅผ ๋งํ๋ค.
strict mode์์๋ ๋งค๊ฐ๋ณ์์ ์ ๋ฌ๋ ์ธ์๋ฅผ ์ฌํ ๋นํ์ฌ ๋ณ๊ฒฝํด๋ arguments ๊ฐ์ฒด์ ๋ฐ์๋์ง ์๋๋ค.
Last updated