Skip to content

Instantly share code, notes, and snippets.

@liujingyu
Last active August 26, 2017 06:05
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 liujingyu/6e77f93982a92d8c3bd9b86325de6a6c to your computer and use it in GitHub Desktop.
Save liujingyu/6e77f93982a92d8c3bd9b86325de6a6c to your computer and use it in GitHub Desktop.
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