ich this book on ES6 Lesen und es ist die folgende:Warum funktioniert als Block beschrieben sind scoped
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.
AFAIK, haben Funktionen immer Funktion scoped. Ich dachte, etwas könnte in ES6 geändert haben, aber nein:
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
Eigentlich scheinen sie Block scoped werden:
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
}
So verändert hat es in ES6?
Ich denke, dass das Buch die (vorhandene) Funktionsdeklaration als Beispiel verwendet, um zu erklären, wie 'let' in das Bild passt. – Lucero
Das liegt daran, dass Sie es im lockeren Modus ausführen. Im strikten Modus sollte es wie erwartet sein. – estus