Chrome
console.log(x);
let x = 1;
// Reference Error: 'x' is not defined
Firefox
console.log(x);
let x = 1;
// Uncaught ReferenceError: can't access lexical declaration 'x' before initializaiton
시작: 변수 스코프의 시작
종료: 변수 선언
{
// Start of foo's TDZ
// foo가 속하는 스코프의 시작 = foo TDZ 시작
let bar = 'bar';
console.log(bar); // "bar"
console.log(foo); // ReferenceError because we're in the TDZ
let foo = 'foo'; // End of foo's TDZ (foo 선언)
}
function frog(foo=bar, bar='bar') { // TDZ
console.log(foo);
}
함수의 매개변수 처리는 좌
-> 우
순서이므로 foo=bar
먼저 처리되므로
{ //TDZ 시작
foo = bar // Reference error
bar = 'bar' //TDZ 끝
}
bar = 'bar'
에 의해 호이스팅이 발생하여 bar
의 선언
이 스코프의 최상단에서 일어난다. 하지만 bar
의 초기화
는 되지 않아 접근이 불가능하다.
foo = bar
는 bar
가 선언되는 코드 위의 영역인 TDZ
안에 있으므로 참조 에러가 발생한다.
Uncaught ReferenceError: Cannot access 'bar' before initialization
at frog (<anonymous>:1:19)
at <anonymous>:1:1