Skip to content

Instantly share code, notes, and snippets.

@gotoweb
Created February 25, 2019 11:55
Show Gist options
  • Save gotoweb/e5d583fe28f0259fcd119c2d5355791f to your computer and use it in GitHub Desktop.
Save gotoweb/e5d583fe28f0259fcd119c2d5355791f to your computer and use it in GitHub Desktop.
callback in for loop: 클로저 및 bind 이용에 관한 문제
var fns = [];
for (var i=0; i<3; i++) {
fns[i] = function() {
console.log('My value:' + i);
}
}
for (var j=0; j<3; j++) {
fns[j]();
}
// My value: 0
// My value: 1
// My value: 2
// ...를 기대했지만,
// My value: 3
// My value: 3
// My value: 3
// ...이 출력됩니다.
// Problem: let 또는 bind method를 이용해서, 이 0,1,2가 순서대로 출력되도록 바꿔보세요.
// 그리고, 왜 이러한 문제가 발생하는지도 이유를 연구해보세요.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment