Last active
October 27, 2015 16:11
-
-
Save wulymammoth/bf53b0b68dab6f254fbd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for (var i = 0; i < 5; i++) { | |
| setTimeout(function() { | |
| console.log(i); | |
| }, 1000); | |
| } | |
| // How do we fix the above to log what we want it to log? | |
| for (var j = 0; j < 5; j++) { | |
| // With an IIFE (immediately-invoked function expression) | |
| // Closures | |
| (function(j) { | |
| setTimeout(function() { | |
| console.log(j); | |
| }, 1000); | |
| })(j); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment