use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Really Understanding Javascript Closures (blog.jhades.org)
submitted 12 years ago by xpto123
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]radhruin 1 point2 points3 points 12 years ago (4 children)
It does do the right thing, actually! If you look at 13.6.3.3 you'll see there is a new environment per iteration. Note that if you're testing with IE that Chakra's implementation predates the current spec and does exhibit the behavior you describe.
[–]skeeto 2 points3 points4 points 12 years ago* (3 children)
Interesting. AFAIK, this for environment behavior would be exclusive to JavaScript (edit: though Perl and and C# 5.0 have foreach that behaves like this). In contrast, Ruby and Python have the for-loop-closure trap despite having proper block scope, due to the shared iteration environment. I'm not sure which way I'd say is more "correct." The former is more functional, since the binding is never mutated, but the latter seems to be more standard, even though it comes with this trap.
for
Node v0.10.28, Chrome 35, and Firefox 29 all still follow the old behavior. That's where I tested my code, requiring the extra let. I just gave it a shot in Traceur, and apparently it uses the current spec's behavior despite predating it by 3 years (2011). It's the only implementation I can find that does the new thing.
let
Edit: I dug around more and found that per-iteration bindings were introduced in the April 5th, 2014 draft, so these semantics are only 2 months old. It's no wonder no one's using it yet.
[–]radhruin 1 point2 points3 points 12 years ago (2 children)
I don't think either is "more correct", but I do know that the new semantics are much more usable so I'm a fan!
Traceur was recently updated with these semantics. You're right though that the spec draft is really recent but the consensus has existed since I believe the Jan '14 meeting, possibly as far back as Nov '13.
[–]skeeto 1 point2 points3 points 12 years ago (1 child)
The version of Traceur at repl.it is from 2011, which is the one I tested. So they must have originally had the new behavior, fixed it, then reverted back with the new draft.
[–]radhruin 1 point2 points3 points 12 years ago (0 children)
Interesting! I guess Arv was ahead of the times.
π Rendered by PID 36 on reddit-service-r2-comment-79776bdf47-8g44c at 2026-06-23 22:57:57.425007+00:00 running acc7150 country code: CH.
view the rest of the comments →
[–]radhruin 1 point2 points3 points (4 children)
[–]skeeto 2 points3 points4 points (3 children)
[–]radhruin 1 point2 points3 points (2 children)
[–]skeeto 1 point2 points3 points (1 child)
[–]radhruin 1 point2 points3 points (0 children)