我读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