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

RESOLVED DUPLICATE of bug 449811

Status

()

Core
JavaScript Engine
RESOLVED DUPLICATE of bug 449811
2 years ago
2 years ago

People

(Reporter: glasserc, Unassigned)

Tracking

Trunk
Points:
---

Firefox Tracking Flags

(firefox50 affected)

Details

Attachments

(1 attachment)

(Reporter)

Description

2 years ago
Created attachment 8761625 [details]
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
Last Resolved: 2 years ago
Resolution: --- → DUPLICATE
Duplicate of bug: 449811
You need to log in before you can comment on or make changes to this bug.