In JavaScript, all binding declarations are instantiated when control flow enters the scope in which they appear. Legacy var and function declarations allow access to those bindings before the actual declaration, with a "value" of undefined. That legacy behavior is known as "hoisting". let and const binding declarations are also instantiated when control flow enters the scope in which they appear, with access prevented until the actual declaration is reached; this is called the Temporal Dead Zone. The TDZ exists to prevent the sort of bugs that legacy hoisting can create.
在Javascript中,所有綁定的聲明當控制流進入他們出現的作用域時被實例化。遺留的var和function聲明允許在實際聲明之前訪問這些綁定的聲明,值為undefined。這個遺留的問題被稱為"hoisting"。let和const綁定的聲明在控制流進入他們出現的作用域時也被實例化,但是有訪問保護直到實際聲明的時候,這稱為臨時性死區(TDZ)。TDZ的存在是為了防止這些提升所導致的bug
note
其實當control flow 進入一個scope的時候
(function () {
console.log(a)
console.log(b)
console.log(typeof foo)
var a
var b
function foo() {}
})()
例如a,b,foo這些變量和函數都會被實例化,使用var聲明的變量和使用function聲明的函數都允許在到實際聲明的那行代碼之前可以訪問這些變量,但是value是undefined,ES6的let,const會有一個access prevented,必需到實際聲明的那行代碼之后才能去訪問,否則會報ReferenceError,這也是為了防止因為hoisting而產生bug