Skip to content

Instantly share code, notes, and snippets.

@haingdc
Created January 30, 2018 10:20
Show Gist options
  • Save haingdc/75f34cf23d9c8a10e2d1cab4e9cadf19 to your computer and use it in GitHub Desktop.
Save haingdc/75f34cf23d9c8a10e2d1cab4e9cadf19 to your computer and use it in GitHub Desktop.
handle closure affect by iife step 2
var result = [];
for(var year = 0; year < 5; year++) {
(function() {
var y = year;
result.push(function() { return y; });
})();
}
console.log(result[1]()); // 1
console.log(result[3]()); // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment