ReferenceError: can't access lexical declaration`X' before initialization
Message
ReferenceError: can't access lexical declaration `X' before initialization (Firefox) ReferenceError: 'x' is not defined (Chrome)
Error type
๋ฌด์์ด ์๋ชป ๋์์๊น์?
๋ณ์๊ฐ ์ด๊ธฐํ ๋๊ธฐ ์ ์ ์์ธ์ค๊ฐ ๋์ด๋ฒ๋ฆฝ๋๋ค. ์ด ๋ฌธ์ ๋ let ๋๋ const ์ ์ธ์ด ์ ์ ๋๊ธฐ ์ ์ ์์ธ์ค๋๋ ๋ชจ๋ block ๋ฌธ์์ ๋ฐ์ํฉ๋๋ค.
Examples
์๋ชป๋ ๊ฒฝ์ฐ
์ด ๊ฒฝ์ฐ์ ๋ณ์ "foo"๋ let ์ ์ฌ์ฉํ์ฌ block ๋ฌธ์์ ๋ค์ ์ ์ธ๋ฉ๋๋ค.
function test() {
let foo = 33;
if (true) {
let foo = (foo + 55);
// ReferenceError: can't access lexical
// declaration `foo' before initialization
}
}
test();
์ฌ๋ฐ๋ฅธ ๊ฒฝ์ฐ
if ๋ฌธ์์ "foo"๋ฅผ ๋ณ๊ฒฝํ๋ ค๋ฉด ์ฌ ์ ์ธ์ ๋ฐ์์ํค๋ let ์ ์ ๊ฑฐํด์ผํฉ๋๋ค.
function test(){
let foo = 33;
if (true) {
foo = (foo + 55);
}
}
test();