2 ms·
Interesting, didn't think about closures in Python before. It seems that JS has the same behavior. Java forces you to final copy the variable.
by sharno 3y ago
Interesting, didn't think about closures in Python before. It seems that JS has the same behavior.
Java forces you to final copy the variable.
- agumonkey 3y agoI lost track of ES scoping rules but apparently `for (var i ...) { ... }` doesn't capture (legacy behavior) while `for (let i ...) { ... }` will.
- mdaniel 3y agoI don't think that's limited to just `var`, as a `let` outside of the `for` will do the same let funcs = []; ;(() => { let i; for (i = 0; i < 10; i++) { funcs.push(function() { console.log('i=',i); }) } })() for (let f of funcs) { f.call(null); } (I used an IIFE to hide the "let i" from the .call to ensure it wasn't late binding to the name like the python example did)