Skip to content

Instantly share code, notes, and snippets.

@mingyang91
Last active September 26, 2015 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingyang91/a56b56c3a7ed3b432e32 to your computer and use it in GitHub Desktop.
Save mingyang91/a56b56c3a7ed3b432e32 to your computer and use it in GitHub Desktop.
function testClosures() {
"use strict";
var result = [];
for (var var_index = 0; var_index < 2; var_index++) {
result.push(() => { return var_index; });
}
for (let let_index = 0; let_index < 2; let_index++) {
result.push(() => { return let_index; });
}
return result;
}
var result = testClosures();
console.log(result[0]()); //2
console.log(result[1]()); //2
console.log(result[2]()); //0
console.log(result[3]()); //1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment