
1. TDZ(Temporal Dead Zone)๋ ๋ฌด์์ธ๊ฐ?
const ๋ณ์๋ฅผ ์ ์ธํ๊ณ ์ด๊ธฐํํ๋ฉด ๋ณ์์ ์ ๊ทผํ ์ ์์ต๋๋ค. ์์ํ๋๋ก ๋์ํฉ๋๋ค.
const white = '#FFFFFF';
white; // => '#FFFFFF'
์ด๋ฒ์๋ ์ ์ธ ์ ์ white ๋ณ์์ ์ ๊ทผํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
white; // throws `ReferenceError`
const white = '#FFFFFF';
white
const white = '#FFFFFF' ๊ตฌ๋ฌธ ์ ์ค๊น์ง, white ๋ณ์๋ TDZ์ ์์ต๋๋ค.
TDZ์ ์๋ white ๋ณ์์ ์ ๊ทผํ๊ฒ ๋๋ฉด , ReferenceError: Cannot access 'white' before initialization ์๋ฐ์คํฌ๋ฆฝํธ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค.

1.1. TDZ์ ์ํฅ์ ๋ฐ๋ ๊ตฌ๋ฌธ
1.1.1. const ๋ณ์
์ด์ ์ ๋ณด์๋ฏ์ด, const ๋ณ์๋ ์ ์ธ ๋ฐ ์ด๊ธฐํ ์ ์ค๊น์ง TDZ์ ์์ต๋๋ค.
// Does not work!
pi; // throws `ReferenceError`
const pi = 3.14;
const ๋ณ์๋ ์ ์ธํ ํ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
const pi = 3.14;
// Works!
pi; // => 3.14
1.1.2. let ๋ณ์
let๋ ์ ์ธ ์ ์ค๊น์ง TDZ์ ์ํฅ์ ๋ฐ์ต๋๋ค.
// Does not work!
count; // throws `ReferenceError`
let count;
count = 10;
๋ค์, let ๋ณ์๋ ์ ์ธ ์ดํ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
let count;
// Works!
count; // => undefined
count = 10;
// Works!
count; // => 10
1.1.3. class ๊ตฌ๋ฌธ
๋จธ๋ฆฌ๋ง ๋ถ๋ถ์์ ๋ณด์๋ฏ์ด, ์ ์ธ ์ ์๋ class๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
// Does not work!
const myNissan = new Car('red'); // throws `ReferenceError`
class Car {
constructor(color) {
this.color = color;
}
}
์ด ์์ ๊ฐ ๋์ํ๋ ค๋ฉด, ํด๋์ค๋ฅผ ์ ์ธํ ํ์ ์ฌ์ฉํ๋๋ก ์์ ํฉ๋๋ค.
class Car {
constructor(color) {
this.color = color;
}
}
// Works!
const myNissan = new Car('red');
myNissan.color; // => 'red'
1.1.4. constructor() ๋ด๋ถ์ super()
๋ถ๋ชจ ํด๋์ค๋ฅผ ์์๋ฐ์๋ค๋ฉด, ์์ฑ์ ์์์ super()๋ฅผ ํธ์ถํ๊ธฐ ์ ๊น์ง this ๋ฐ์ธ๋ฉ์ TDZ์ ์์ต๋๋ค.
class MuscleCar extends Car {
constructor(color, power) {
this.power = power;
super(color);
}
}
// Does not work!
const myCar = new MuscleCar(โblueโ, โ300HPโ); // `ReferenceError`
์ด ์ฝ๋๋ฅผ ๋ณด๋ฉด constructor() ์์์ super()๊ฐ ํธ์ถ๋๊ธฐ ์ ๊น์ง this๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
TDZ๋ ์ธ์คํด์ค๋ฅผ ์ด๊ธฐํํ๊ธฐ ์ํด ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์๋ฅผ ํธ์ถํ ๊ฒ์ ์ ์ํฉ๋๋ค. ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์๋ฅผ ํธ์ถํ๊ณ ์ธ์คํด์ค๊ฐ ์ค๋น๋๋ฉด ์์ ํด๋์ค์์ this ๊ฐ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
class MuscleCar extends Car {
constructor(color, power) {
super(color);
this.power = power;
}
}
// Works!
const myCar = new MuscleCar('blue', '300HP');
myCar.power; // => '300HP'
1.1.5. ๊ธฐ๋ณธ ํจ์ ๋งค๊ฐ๋ณ์(Default Function Parameter)
๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์๋ ๊ธ๋ก๋ฒ๊ณผ ํจ์ ์ค์ฝํ ์ฌ์ด์ ์ค๊ฐ ์ค์ฝํ(intermidiate scope)์ ์์นํฉ๋๋ค. ๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์ ๋ํ TDZ ์ ํ์ด ์์ต๋๋ค.
const a = 2;
function square(a = a) {
return a * a;
}
// Does not work!
square(); // throws `ReferenceError`
๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์ a๋ ์ ์ธ ์ ์ a = a ํํ์์ ์ค๋ฅธ์ชฝ์์ ์ฌ์ฉ๋์์ต๋๋ค. a์์ ์ฐธ์กฐ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค.
๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์๋ ์ ์ธ ๋ฐ ์ด๊ธฐํ ๋ค์์ ์ฌ์ฉ๋์ด์ผ ํฉ๋๋ค. ์ด ๊ฒฝ์ฐ init๊ณผ ๊ฐ์ ๋ค๋ฅธ ๋ณ์๋ก ์ ์ธํ์ฌ ์ฌ์ฉํฉ๋๋ค.
const init = 2;
function square(a = init) {
return a * a;
}
// Works!
square(); // => 4
1.2. var, function, import ๊ตฌ๋ฌธ
์์์ ์ค๋ช ํ ๊ฒ๋ค๊ณผ ๋ฐ๋๋ก var, function ์ ์ธ์ TDZ์ ์ํฅ์ ๋ฐ์ง ์์ต๋๋ค. ์ด๊ฒ๋ค์ ํ์ฌ ์ค์ฝํ์์ ํธ์ด์คํ ๋ฉ๋๋ค.
var ๋ณ์๋ ์ ์ธํ๊ธฐ ์ ์ ์ ๊ทผํ๋ฉด, undefined๋ฅผ ์ป๊ฒ ๋ฉ๋๋ค.
// Works, but don't do this!
value; // => undefined
var value;
๊ทธ๋ฌ๋ ํจ์๋ ์ ์ธ๋ ์์น์ ์๊ด์์ด ๋์ผํ๊ฒ ํธ์ถ๋ฉ๋๋ค.
// Works!
greet('World'); // => 'Hello, World!'
function greet(who) {
return `Hello, ${who}!`;
}
// Works!
greet('Earth'); // => 'Hello, Earth!'
๋น์ ์ ํจ์ ๊ตฌํ๋ณด๋ค ํธ์ถ์ ๋ ๊ด์ฌ์ด ์๊ธฐ ๋๋ฌธ์ ์ข ์ข ํจ์ ์ ์ธ ์ ์ ํธ์ถํ๊ฒ ๋ฉ๋๋ค. ํจ์ ์ ์ธ ์ ์ ํธ์ถํด๋ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋ ์ด์ ๋ ํธ์ด์คํ ๋๋ฌธ์ ๋๋ค.
ํฅ๋ฏธ๋ก์ด ์ ์ผ๋ก import ๋ชจ๋ ์ญ์ ํธ์ด์คํ ๋ฉ๋๋ค.
// Works!
myFunction();
import { myFunction } from './myModule';
import ๊ตฌ๋ฌธ์ด ํธ์ด์คํ ๋๊ธฐ ๋๋ฌธ์, ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ ์์ ๋ถ๋ถ์์ ๋ํ๋์ ๋ชจ๋์ ๊ฐ์ ธ์ค๋ ๊ฒ์ด ์ข์ต๋๋ค.
1.3. TDZ์์ typeof ์ฐ์ฐ์์ ๋์
typeof ์ฐ์ฐ์๋ ๋ณ์๊ฐ ํ์ฌ ์ค์ฝํ ์์ ์ ์ธ๋์๋์ง ํ์ธํ ๋ ์ ์ฉํฉ๋๋ค.
์๋ฅผ ๋ค์ด์, notDefined ๋ณ์๋ ์ ์ธ๋์ง ์์์ต๋๋ค. ์ด ๋ณ์์ typeof ์ฐ์ฐ์๋ฅผ ์ ์ฉํ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค.
typeof notDefined; // => 'undefined'
๋ณ์๊ฐ ์ ์ธ๋์ง ์์๊ธฐ ๋๋ฌธ์, typeof notDefined๋ undefined๋ก ํ๊ฐํฉ๋๋ค.
๊ทธ๋ฌ๋ TDZ์ ๋ณ์์์ typeof ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ค๋ฅด๊ฒ ๋์ํฉ๋๋ค. ๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค.
typeof variable; // throws `ReferenceError`
let variable;
1.4. ํ์ฌ ์ค์ฝํ ์์์ TDZ ๋์
TDZ์ ์ ์ธ๋ฌธ์ด ์กด์ฌํ๋ ์ค์ฝํ ๋ฒ์ ์์์ ๋ณ์์ ์ํฅ์ ์ค๋๋ค.

์์ ๋ฅผ ๋ณด๊ฒ๋๋ฉด,
function doSomething(someVal) {
// Function scope
typeof variable; // => undefined
if (someVal) {
// Inner block scope
typeof variable; // throws `ReferenceError`
let variable;
}
}
doSomething(true);
์ด ์ฝ๋๋ 2๊ฐ์ ์ค์ฝํ๋ฅผ ๊ฐ์ง๋๋ค.
- ํจ์ ์ค์ฝํ
- let ๋ณ์๊ฐ ์ ์ธ๋ ๋ด๋ถ ๋ธ๋ก ์ค์ฝํ
ํจ์ ์ค์ฝํ์์ typeof variable
๋ undefined
๋ก ํ๊ฐ๋ฉ๋๋ค. ์ฌ๊ธฐ์๋ let variable
๊ตฌ๋ฌธ์ TDZ์ ์ํฅ์ ์ฃผ์ง ์์ต๋๋ค.
typeof variable
๊ตฌ๋ฌธ์ ๋ด๋ถ ์ค์ฝํ์์๋ ์ ์ธ ์ ์ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ฉด ReferenceError: Cannot access 'variable' before initialization ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค. TDZ๋ ๋ด๋ถ ์ค์ฝํ์์๋ง ์กด์ฌํฉ๋๋ค.
2. References
TDZ์ ๋ชจ๋ฅธ ์ฑ ์๋ฐ์คํฌ๋ฆฝํธ ๋ณ์๋ฅผ ์ฌ์ฉํ์ง ๋ง๋ผ
๊ฐ๋จํ ์ง๋ฌธ์ ํ๋ ํ๊ฒ ๋ค. ์๋ ์ฝ๋ ์ค๋ํซ์์ ์๋ฌ๊ฐ ๋ฐ์ํ ๊น? ์ฒซ ๋ฒ์งธ ์ฝ๋๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋ค์ ํด๋์ค๋ฅผ ์ ์ธํ๋ค.
ui.toast.com
Don't Use JavaScript Variables Without Knowing Temporal Dead Zone
Temporal Dead Zone forbids the access of variables and classes before declaration in JavaScript.
dmitripavlutin.com
'๐ฅ Front-End > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] ES6 (0) | 2022.07.24 |
---|---|
[JS] ์คํ ์ปจํ ์คํธ, ํธ์ด์คํ , ํจ์ ์ ์ธ๋ฌธ๊ณผ ํจ์ ํํ์ (0) | 2022.07.21 |
[JS] ํด๋ก์ (Closure) (0) | 2022.07.21 |
[JS] ์ค์ฝํ(Scope) (0) | 2022.07.21 |
[JS] JavaScript ๊ฐ์ฒด์ ๋ถ๋ณ์ฑ (0) | 2022.07.20 |