Closed Bug 1071877 Opened 6 years ago Closed 3 years ago

Support ES6 block level function declarations in strict mode

Categories

(Core :: JavaScript Engine, defect)

x86_64
Linux
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 1071646

People

(Reporter: jorendorff, Unassigned)

References

Details

In SM strict mode code, a FunctionDeclaration can't be appear immediately inside a Block. You get a SyntaxError.

In ES6 draft rev 27, in strict mode code, a FunctionDeclaration can occur in a Block. It's a block-local function, hoisted to the top of the block.

Strict mode only because of web compatibility. Bug 585536 comment 48 has some of the dream-crushing details.
In short, this just works how you'd expect. It's pretty great.

But to make it concrete, an example:

    1   "use strict";
    2   for (let x of obj) {
    3       f();
    4       function f() { /* ... code that refers to f and x ... */ }
    5   }

Each time through the loop, as we enter the Block starting with the curly brace at the end of line 2, we create a new function f and bind it to a new block-scoped variable 'f', then evaluate the StatementList inside the Block.

Because the binding 'f' is initialized before the StatementList starts to run, there's no TDZ. It's OK to call f() on line 3, ahead of the declaration of f on line 4.

All this is more or less there in ES6 draft rev 27, though it's beautifully obfuscated.
See Also: → 585536
Keywords: dev-doc-needed
Whiteboard: [DocArea=JS]
Duplicate of this bug: 434912
Status: NEW → RESOLVED
Closed: 3 years ago
Resolution: --- → DUPLICATE
Duplicate of bug: 1071646
Keywords: dev-doc-needed
Whiteboard: [DocArea=JS]
You need to log in before you can comment on or make changes to this bug.