2.1.35 [ECMA-262/6] Section 13.7.5.13 Runtime Semantics: ForIn/OfBodyEvaluation ( lhs, stmt, iterator, lhsKind, labelSet )
V0171: Lexical declarations in for-of/for-in loops do not get new environments for each iteration of the loop
The specification states:
-
13.7.5.13 Runtime Semantics: ForIn/OfBodyEvaluation ( lhs, stmt, iterator, lhsKind, labelSet ) The abstract operation ForIn/OfBodyEvaluation is called with arguments lhs, stmt, iterator, lhsKind, and labelSet. The value of lhsKind is either assignment, varBinding or lexicalBinding. 1. Let oldEnv be the running execution context’s LexicalEnvironment. 2. Let V = undefined . 3. Let destructuring be IsDestructuring of lhs. 4. If destructuring is true and if lhsKind is assignment, then a. Assert: lhs is a LeftHandSideExpression. b. Let assignmentPattern be the parse of the source text corresponding to lhs using AssignmentPattern as the goal symbol. 5. Repeat a. Let nextResult be IteratorStep(iterator). b. ReturnIfAbrupt(nextResult). c. If nextResult is false, return NormalCompletion(V). d. Let nextValue be IteratorValue(nextResult). e. ReturnIfAbrupt(nextValue). f. If lhsKind is either assignment or varBinding, then I. If destructuring is false, then 1. Let lhsRef be the result of evaluating lhs ( it may be evaluated repeatedly). g. Else i. Assert: lhsKind is lexicalBinding. ii. Assert: lhs is a ForDeclaration. iii. Let iterationEnv be NewDeclarativeEnvironment(oldEnv). iv. Perform BindingInstantiation for lhs passing iterationEnv as the argument. v. Set the running execution context’s LexicalEnvironment to iterationEnv. vi. If destructuring is false, then 1. Assert: lhs binds a single name. 2. Let lhsName be the sole element of BoundNames of lhs. 3. Let lhsRef be ResolveBinding(lhsName). 4. Assert: lhsRef is not an abrupt completion. ...
IE11 Mode (All versions)
Steps 5f and 5g are replaced by the substeps of 5f only:
f. (nothing)
i. If destructuring is false, then
1. Let lhsRef be the result of evaluating lhs ( it may be evaluated repeatedly).
g. (nothing)
h. ...
Therefore, lexical declarations in for-of
/for-in
loops do not get new environments per iteration
of the loop.