Skip to content

Instantly share code, notes, and snippets.

@mingyang91
Last active Sep 26, 2015
Embed
What would you like to do?
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