3 ms·
I lost track of ES scoping rules but apparently `for (var i ...) { ... }` doesn't capture (legacy behavior) while `for (let i ...) { ... }` will.
by agumonkey 3y ago
I 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)