javascript闭包的解决方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">

        /*以下是从Prototype框架中截取的代码---------------------------------------*/
       
        var $A = function(iterable) {
            if (!iterable) return [];
            if (iterable.toArray) {
                return iterable.toArray();
            } else {
                var results = [];
                for (var i = 0, length = iterable.length; i < length; i++)
                    results.push(iterable[i]);
                return results;
            }
        }

        //闭包方法(第一个参数为绑定方法所属的对象,后面参数为绑定方法的参数)。
        Function.prototype.bind = function() {
            var __method = this, args = $A(arguments), object = args.shift();
            return function() {
                return __method.apply(object, args.concat($A(arguments)));
            }
        }

        /*-------------------------------------------------------------------*/

        function Node(id, name, cn) {
            this.id = id;
            this.name = name;
            this.cn = cn;
        }
        var nodes = new Array();
        nodes[0] = new Node(1, "node1", "节点1");
        nodes[1] = new Node(2, "node2", "节点2");


        var nodeConfig = {};
        nodeConfig.ticks = 4 * 1000; //刷新频率
        nodeConfig.msg = "运行中";

        function showStatus(node) {
            alert(node.name + ":" + nodeConfig.msg);
        }

        function start() {
            var len = nodes.length;
            for (var i = 0; i < len; i++) {
             //闭包解决。
                var bindFun = showStatus.bind(null, nodes[i]);
                setInterval(bindFun, nodeConfig.ticks);
            }
        }

        start();           
       
       
    </script>

</head>
<body>
    <input id="Button1" type="button" value="button" onclick="tt();" />
</body>
</html>


参考文章:http://www.cnblogs.com/KevinYang/archive/2009/07/14/1522915.html
原文地址:https://www.cnblogs.com/mrhgw/p/1550768.html