• 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
  • 相关阅读:
    oracle中查询或插入特殊字符
    html最多显示两行,css 实现两行或多行文本溢出显示省略号(...)
    RT
    发表一个自己做的WPF游戏
    用silverlight制作自己的GIS
    一个字符串切割问题
    Oracle所有者权限与调用者权限(转)
    Oracle角色权限的使用事项(转)
    Tomcat 内存溢出 详解
    forms验证:怎么验证两种身份?
  • 原文地址:https://www.cnblogs.com/mrhgw/p/1550768.html
Copyright © 2020-2023  润新知