我讀this book on ES6,並有以下幾點:爲什麼功能描述塊範圍的
Function declarations…
- are block-scoped, like let.
- create properties in the global object (while in global scope), like var.
- are hoisted: independently of where a function declaration is mentioned in its scope, it is always created at the beginning of the scope.
據我所知,功能一直是功能範圍的。我想可能在ES6已經改變了,但沒了:
function a() {
if (true) {
// defined inside the block and is hoisted to the top of that block
z();
function z() { console.log ('z')}
}
z();
}
// but is also hoisted to the function scope
a(); // works OK
事實上,他們似乎是塊作用域:
function a() {
if (false) {
// defined inside the block and is hoisted to the top of that block
z();
function z() { console.log ('z')}
}
z(); // error
}
所以已經在它ES6變化?
我認爲這本書使用(現有)函數聲明作爲例子來解釋'let'如何適合圖片。 – Lucero
那是因爲你在鬆散模式下執行它。在嚴格模式下,它應該如預期的那樣。 – estus