Closed ghost closed 3 years ago
for (var i=1; i<=5; i++) {
(function(i){
setTimeout( function timer(){
console.log( i );
}, i*1000 );
})(i);
}
You need take i
into IIFE
@osaimi if you run your snippet, you'll get this result:
It doesn't fail, but it's not right either, as the "expected" outcome is this (from @JaykeyGuo's snippet illustrates):
But it shouldn't even work because IIFE creates a new scope that doesn't have 'i'.
Am I missing something here?
I changed var to let for the sake of experiment and if behaved as expected!!
Creating an IIFE that has no declared variables in it is a no-op... it has no effect... as your initial snippet showed. If you removed the IIFE, nothing would change.
Using let
creates a different kind of binding (block scoping) per iteration, so again the IIFE is completely irrelevant... take it out or leave it, makes no difference. An empty scope is no different than no scope at all, in this respect.
It makes sense now. Thank you @getify
Edition: 1st
Book Title: Scope & Closures
Chapter: 5
Section Title: Loops + Closure
Problem: I ran this snippet of code in my browser, Google Chrome, and it works just fine.
apparently, the scope is not empty!!
I'm confused 🤔