Create a gist now

Instantly share code, notes, and snippets.

@liujingyu /function.js Secret
Last active Aug 26, 2017

Embed
What would you like to do?
javascript闭包作用域
for (var i = 0; i < 5; i++) {
setTimeout(function () {
console.log(i);
}, 5);
}
for (var i = 0; i < 5; i++) {
(function (idx) {
setTimeout(function () {
console.log(idx);
}, 5);
})(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment