• <legend id='YzerF'><style id='YzerF'><dir id='YzerF'><q id='YzerF'></q></dir></style></legend>
    <i id='YzerF'><tr id='YzerF'><dt id='YzerF'><q id='YzerF'><span id='YzerF'><b id='YzerF'><form id='YzerF'><ins id='YzerF'></ins><ul id='YzerF'></ul><sub id='YzerF'></sub></form><legend id='YzerF'></legend><bdo id='YzerF'><pre id='YzerF'><center id='YzerF'></center></pre></bdo></b><th id='YzerF'></th></span></q></dt></tr></i><div id='YzerF'><tfoot id='YzerF'></tfoot><dl id='YzerF'><fieldset id='YzerF'></fieldset></dl></div>
    <tfoot id='YzerF'></tfoot>

        • <bdo id='YzerF'></bdo><ul id='YzerF'></ul>
      1. <small id='YzerF'></small><noframes id='YzerF'>

        javascript:在函数(){}中使用当前的for循环计数器值?

        javascript: Using the current for-loop counter-value inside a function() { }?(javascript:在函数(){}中使用当前的for循环计数器值?)

          <i id='2sMfl'><tr id='2sMfl'><dt id='2sMfl'><q id='2sMfl'><span id='2sMfl'><b id='2sMfl'><form id='2sMfl'><ins id='2sMfl'></ins><ul id='2sMfl'></ul><sub id='2sMfl'></sub></form><legend id='2sMfl'></legend><bdo id='2sMfl'><pre id='2sMfl'><center id='2sMfl'></center></pre></bdo></b><th id='2sMfl'></th></span></q></dt></tr></i><div id='2sMfl'><tfoot id='2sMfl'></tfoot><dl id='2sMfl'><fieldset id='2sMfl'></fieldset></dl></div>

          <legend id='2sMfl'><style id='2sMfl'><dir id='2sMfl'><q id='2sMfl'></q></dir></style></legend>
        1. <small id='2sMfl'></small><noframes id='2sMfl'>

        2. <tfoot id='2sMfl'></tfoot>

            • <bdo id='2sMfl'></bdo><ul id='2sMfl'></ul>
                <tbody id='2sMfl'></tbody>

                  本文介绍了javascript:在函数(){}中使用当前的for循环计数器值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我想这样做的网站上:(简化)

                  on a website i want to do this: (simplified)

                  myHandlers = new Array();
                  for(var i = 0; i < 7; i++) {
                    myHandlers.push(new Handler({
                      handlerName: 'myHandler'+i, // works, e.g. ->myHandler1, 2, 3 etc.
                      handlerFunc: function(bla) { /*...*/ alert(i); } // doesn't work,all return 7
                    }
                  }
                  

                  我可以将计数器设置为我的 Handler 的另一个属性(它将复制当前值)并在我的函数中使用它,但我想,还有一种方法可以实际复制这个值,不是吗?

                  I could set the counter as another attribute of my Handler (which would copy the current value) and use it inside my function, but I guess, there is also a way to actually copy this value, no?

                  推荐答案

                  当调用handlerFunc时,函数内部的i指的是ifor 循环的代码>.但是那个 i 可能不再具有相同的值了.

                  When handlerFunc is called, the i inside the function refers to the i of the for loop. But that i does probably not have the same value any more.

                  使用闭包将 i 的当前值绑定到匿名函数的范围内:

                  Use a closure to bind the current value of i in the scope of an anonymous function:

                  handlerFunc: (function(i) { return function(bla) { /*...*/ alert(i); }; })(i)
                  

                  这里使用了一个匿名函数 (function(i) { … })(i) 并立即调用.该函数将 for 循环的 i 的值绑定到本地 i.该 i 然后独立于 for 循环的 i.

                  Here an anonymous function (function(i) { … })(i) is used and called immediately. This function binds the value of i of the for loop to the local i. That i is then independent from the i of the for loop.

                  这篇关于javascript:在函数(){}中使用当前的for循环计数器值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                  • <small id='afuLv'></small><noframes id='afuLv'>

                  • <i id='afuLv'><tr id='afuLv'><dt id='afuLv'><q id='afuLv'><span id='afuLv'><b id='afuLv'><form id='afuLv'><ins id='afuLv'></ins><ul id='afuLv'></ul><sub id='afuLv'></sub></form><legend id='afuLv'></legend><bdo id='afuLv'><pre id='afuLv'><center id='afuLv'></center></pre></bdo></b><th id='afuLv'></th></span></q></dt></tr></i><div id='afuLv'><tfoot id='afuLv'></tfoot><dl id='afuLv'><fieldset id='afuLv'></fieldset></dl></div>
                        <tbody id='afuLv'></tbody>

                    1. <legend id='afuLv'><style id='afuLv'><dir id='afuLv'><q id='afuLv'></q></dir></style></legend>

                      • <bdo id='afuLv'></bdo><ul id='afuLv'></ul>
                      • <tfoot id='afuLv'></tfoot>