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中悲没,所有綁定的聲明當(dāng)控制流進(jìn)入他們出現(xiàn)的作用域時(shí)被實(shí)例化。遺留的var和function聲明允許在實(shí)際聲明之前訪問這些綁定的聲明,值為undefined穆趴。這個(gè)遺留的問題被稱為"hoisting"策治。let和const綁定的聲明在控制流進(jìn)入他們出現(xiàn)的作用域時(shí)也被實(shí)例化蟆技,但是有訪問保護(hù)直到實(shí)際聲明的時(shí)候,這稱為臨時(shí)性死區(qū)(TDZ)只盹。TDZ的存在是為了防止這些提升所導(dǎo)致的bug
note
其實(shí)當(dāng)control flow 進(jìn)入一個(gè)scope的時(shí)候
(function () {
console.log(a)
console.log(b)
console.log(typeof foo)
var a
var b
function foo() {}
})()
例如a衣形,b驼侠,foo這些變量和函數(shù)都會(huì)被實(shí)例化,使用var聲明的變量和使用function聲明的函數(shù)都允許在到實(shí)際聲明的那行代碼之前可以訪問這些變量泵喘,但是value是undefined泪电,ES6的let,const會(huì)有一個(gè)access prevented纪铺,必需到實(shí)際聲明的那行代碼之后才能去訪問相速,否則會(huì)報(bào)ReferenceError,這也是為了防止因?yàn)閔oisting而產(chǎn)生bug