Skip to content

Instantly share code, notes, and snippets.

@junichiro
Created December 4, 2009 06:18
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 junichiro/248858 to your computer and use it in GitHub Desktop.
Save junichiro/248858 to your computer and use it in GitHub Desktop.
// bad
var add_the_handlers = function (nodes) {
var i;
for (i = 0; i < nodes.length; i += 1) {
nodes[i].onclick = function () {
alert(i);
}
}
};
add_the_handlers(document.body.childNodes);
// good
var add_the_handlers = function (nodes) {
var i;
for (i = 0; i < nodes.length; i += 1) {
nodes[i].onclick = function (i) {
return function () {
alert(i);
}
}(i);
}
};
add_the_handlers(document.body.childNodes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment