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