相关疑难解决方法(0)

闭包:"Javascript:Good Parts"示例的逐行说明?

我正在阅读"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)

javascript methods closures function

11
推荐指数
1
解决办法
2261
查看次数

标签 统计

closures ×1

function ×1

javascript ×1

methods ×1