我正在阅读"Javascript:The Good Parts",我对这里发生的事情感到非常困惑.将非常感谢更详细和/或简化的解释.
// BAD EXAMPLE
// Make a function that assigns event handler functions to an array of nodes the wrong way.
// When you click on a node, an alert box is supposed to display the ordinal of the node.
// But it always displays the number of nodes instead.
var add_the_handlers = function (nodes) {
var i;
for (i = 0; i < nodes.length; i += 1) {
nodes[i].onclick = function (e) {
alert(i);
}
}
}; …Run Code Online (Sandbox Code Playgroud)