자바스크립트 기초 D1

nearworld·2022년 7월 30일
0

javascript

목록 보기
1/7

브라우저에 따라 다른 에러 메세지

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

TDZ (Temporal Dead Zone)

시작: 변수 스코프의 시작
종료: 변수 선언

{
 	// 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 선언)
}

매개변수 TDZ

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 = barbar가 선언되는 코드 위의 영역인 TDZ안에 있으므로 참조 에러가 발생한다.

Uncaught ReferenceError: Cannot access 'bar' before initialization
    at frog (<anonymous>:1:19)
    at <anonymous>:1:1
profile
깃허브: https://github.com/nearworld

0개의 댓글