Closed Bug 1279251 Opened 3 years ago Closed 3 years ago

for(let i in x) scope is different than for(let i = 0; i < 5; i++)

Categories

(Core :: JavaScript Engine, defect)

defect
Not set

Tracking

()

RESOLVED DUPLICATE of bug 449811
Tracking Status
firefox50 --- affected

People

(Reporter: glasserc, Unassigned)

Details

Attachments

(1 file)

Attached file closure-scope.js
The MDN page for "let" (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let) has an example where a "let" declaration in a for loop initializer causes the declared variable to be closed over in closures.

However, if I change the "for(let i = ....; ...; ...)" loop into a "for(let i in ...)" or "for(let i of ...)" loop, the scoping behavior is different; the declared variable is shared across closures.

I don't have enough understanding of ECMAScript 6 to know if this behavior is desire/correct or not. However, V8 does not have this difference.

Attached is an example program which demonstrates the two loop styles and their behavior. It's also available as JSFiddle at https://jsfiddle.net/8myyhobx/ .
Our for-in/of loop scoping is wrong in a number of different ways right now.  Bug 449811 is one of several related bugs for the different issues, and might even be the one most appropriate for a dup-mark, but I'm too lazy to check for absolute certain right now.  :-)
Status: NEW → RESOLVED
Closed: 3 years ago
Resolution: --- → DUPLICATE
Duplicate of bug: 449811
You need to log in before you can comment on or make changes to this bug.