[REAL Deep Dive into JS] 20. strict mode

young_palleteยท2022๋…„ 9์›” 18์ผ
0

REAL JavaScript Deep Dive

๋ชฉ๋ก ๋ณด๊ธฐ
20/46
post-custom-banner

๐Ÿšฆ ๋ณธ๋ก 

ํ”ํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋น„์—„๊ฒฉ ๋ชจ๋“œ

์—„๊ฒฉ ๋ชจ๋“œ! ์ƒ๊ฐ๋ณด๋‹ค ์ต์ˆ™ํ•œ ์ฃผ์ œ๋„ค์š” ๐Ÿ™‡๐Ÿปโ€โ™‚๏ธ
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ๋น„์—„๊ฒฉ ๋ชจ๋“œ๋กœ ๊ฐœ๋ฐœํ•  ์‹œ ๊ดด์ƒํ•œ ์ผ๋“ค์ด ์ •~๋ง ๋งŽ์ด ๋ฒŒ์–ด์ง‘๋‹ˆ๋‹ค.

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

์„ ์–ธํ•˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๋ฅผ ๋“ฑ๋กํ•  ๋•Œ ์•”๋ฌต์ ์œผ๋กœ ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋กœ ํ• ๋‹นํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ์ฝ”๋“œ๋Š” ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค!

function test() {
	NO_DECLARE_VAR = 'test';
}

console.log(NO_DECLARE_VAL); // test

๋ณ€์ˆ˜, ํ•จ์ˆ˜, ๋งค๊ฐœ๋ณ€์ˆ˜ ์‚ญ์ œ ๋ช…๋ น ์‹œ ์—๋Ÿฌ X

์ž˜ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์•”๋ฌต์  ์ „์—ญ์—์„œ ํŒŒ์ƒ๋œ ๋ฌธ์ œ์—์š”.
๊ฒฐ๊ตญ ์•”๋ฌต์  ์ „์—ญ์€ ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋กœ ํ• ๋‹นํ•˜๋Š” ๊ฑฐ์ฃ ?
๋”ฐ๋ผ์„œ delete๋กœ ๋ณ€์ˆ˜, ํ•จ์ˆ˜๋ฅผ ์‚ญ์ œํ•  ์ˆ˜๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค. ํ—ˆํ—ˆ... ๐Ÿ˜ญ

๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์—„๋ฐ€ํžˆ ๋งํ•˜์ž๋ฉด ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์กด์žฌํ•˜๋Š” ์ง€์—ญ ๋ณ€์ˆ˜์ด๋ฏ€๋กœ ์‚ญ์ œ๊ฐ€ ๋˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค๋งŒ, ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์€ ๊ฐœ๋ฐœ์ž๋“ค ๊ฐ„์˜ ์ฝ”๋“œ ๋ถ„์„์„ ํ—ท๊ฐˆ๋ฆฌ๊ฒŒ ํ•˜๋Š” ์š”์†Œ๊ธฐ๋„ ํ•˜์ฃ !

function test() {
	a = 1;
    delete a;
}
console.log(a) // Reference Error: a is not defined

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

์ด๊ฑด ์ €๋„ ๋ชฐ๋ž๋Š”๋ฐ์š”!
๋งค๊ฐœ ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ์ค‘๋ณต๋˜๋„ ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š๋Š”๊ตฐ์š”?!
์ด๋•Œ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ์ค‘๋ณต๋œ ์ด๋ฆ„ ์ค‘ ๋งˆ์ง€๋ง‰ ์ธ์ˆ˜์— ์ ์šฉ๋œ๋‹ต๋‹ˆ๋‹ค!


function foo(x, x) {
  console.log(x)
}

foo(3,4)

with ๋ฌธ์˜ ์‚ฌ์šฉ

with... ์•„๋ฌด๋ž˜๋„ ์–ด๋–ค ๋ณ€์ˆ˜๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด ์˜ณ์€์ง€๋ฅผ ํŒ๋‹จํ•˜๊ธฐ๊ฐ€ ์• ๋งคํ•ด์„œ ๋”์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š์ฃ .

ํ•˜์ง€๋งŒ ๋น„์—„๊ฒฉ ๋ชจ๋“œ๋Š” with๋„ ์–ธ์ œ๋“ ์ง€ ํ™˜์˜์ž…๋‹ˆ๋‹ค! ๐Ÿ™†๐Ÿป

function test(arg) {
	with(arg) {
    	console.log(arg)
    }
};

test({ arg: 'test!' }); 
// result value: test!; 
// expected value: { arg: 'test!' }๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•จ.

arguments ๊ฐ์ฒด

๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ, arguments์˜ ๊ฐ’๋„ ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค๋Š” ์‚ฌ์‹ค! ์•Œ๊ณ  ๊ณ„์…จ๋‚˜์š”? ๐Ÿ‘€

function test(a) {
  a = 3
  console.log(arguments)
}

test(1); // [Arguments] { '0': 3 }

์—„๊ฒฉ๋ชจ๋“œ์˜ ๋“ฑ์žฅ๊ณผ ํ•œ๊ณ„

์ด๋Ÿฐ ๋ฌธ์ œ๋“ค ๋•Œ๋ฌธ์— ์—„๊ฒฉ ๋ชจ๋“œ๋Š” ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์–ด์š”!

ํ•จ์ˆ˜ ๋ชธ์ฒด ๋‚ด๋ถ€์˜ ๋งจ ์œ„๋‚˜, ์ „์—ญ ์ฝ”๋“œ์˜ ๋งจ ์œ„์— strict mode๋ฅผ ์„ ์–ธํ•ด์ฃผ์„ธ์š”. ๊ทธ๋ ‡๊ฒŒ ํ•œ๋‹ค๋ฉด ์ง€์—ญ / ์ „์—ญ์œผ๋กœ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ์–ด์š”. ๐Ÿ˜‰

function test(a) {
  a = 3
  console.log(arguments)
}

test(1); // [Arguments] { '0': 1 }

์ค‘์š”ํ•œ ๊ฑด, use strict๋Š” ํ•จ์ˆ˜ ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ธ”๋ก ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„์—์„œ์˜ ์Šค์ฝ”ํ”„๋Š” ์ ์šฉ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์„ธ์š”!

{
  "use strict";
  function test(a) {
    a = 3
    console.log(arguments)
  }
}

test(1); // [Arguments] { '0': 3 }

ํ•œ๊ณ„

๋‹ค๋งŒ ์ด๋Ÿฌํ•œ use strict๋Š” ์ „์—ญ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ์จ๋“œ ํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ non-strict mode์ผ ๊ฒฝ์šฐ์— ์›์น˜ ์•Š๋Š” ์˜ํ–ฅ์„ ์ฃผ๊ธฐ๋„ ํ•ด์š”.

๋”ฐ๋ผ์„œ ์ง€์—ญ์ ์œผ๋กœ ์“ฐ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ˜– ์—‡, ๊ณ„์† ํ•จ์ˆ˜ ์“ธ ๋•Œ๋งˆ๋‹ค ๋น„์—„๊ฒฉ ๋ชจ๋“œ๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฑฐ ๋„ˆ๋ฌด ๊ท€์ฐฎ์€๋ฐ์š”?!

๋”ฐ๋ผ์„œ ์ •์  ๋ถ„์„ ๋„๊ตฌ์ธ ๋ฆฐํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ๊ทœ์น™์„ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ด์š”!
๋Œ€ํ‘œ์ ์œผ๋กœ ESLint๊ฐ€ ์žˆ๋‹ต๋‹ˆ๋‹ค ๐Ÿ˜

๐ŸŒˆ ๋งˆ์น˜๋ฉฐ

์‚ฌ์‹ค ์ƒ๊ฐ๋ณด๋‹ค "use strict"๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์จ์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†๊ธด ํ•˜๋„ค์š”.
ESLint๊ฐ€ ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ•๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. (ESLint์˜ ๋ฃฐ์„ ์–ด๊ธด๋‹ค๋ฉด ์—๋Ÿฌ๋ฅผ ๋‚ด๋ฑ‰๋„๋ก ํ•˜๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ ๋“ฑ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋”์šฑ ์•ˆ์ •์„ฑ ์žˆ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์šด์˜์ด ๊ฐ€๋Šฅํ•˜๊ธฐ๋„ ํ•ด์š”!)

๊ทธ๋Ÿฐ๋ฐ ๊ฐ€๋”, ๊ฐ„๋‹จํ•˜๊ฒŒ ํ…Œ์ŠคํŠธ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋• ESLint๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ๊ฐ€ ์žˆ๋Š”๋ฐ์š”, ์ด๋Ÿด ๋•Œ์—๋Š” ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•  ๊ฒƒ ๊ฐ™์•„์š”. ์ด์ƒ!

profile
People are scared of falling to the bottom but born from there. What they've lost is nth. ๐Ÿ˜‰
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€