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

    1. <tfoot id='P3Is4'></tfoot>

        • <bdo id='P3Is4'></bdo><ul id='P3Is4'></ul>
      1. <legend id='P3Is4'><style id='P3Is4'><dir id='P3Is4'><q id='P3Is4'></q></dir></style></legend>

        <small id='P3Is4'></small><noframes id='P3Is4'>

      2. 为什么使用“for...in"?数组迭代是个坏主意?

        Why is using quot;for...inquot; for array iteration a bad idea?(为什么使用“for...in?数组迭代是个坏主意?)

          <tbody id='XJoQN'></tbody>

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

            <legend id='XJoQN'><style id='XJoQN'><dir id='XJoQN'><q id='XJoQN'></q></dir></style></legend>
              <bdo id='XJoQN'></bdo><ul id='XJoQN'></ul>

              <small id='XJoQN'></small><noframes id='XJoQN'>

                1. 本文介绍了为什么使用“for...in"?数组迭代是个坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有人告诉我不要在 JavaScript 中将 for...in 与数组一起使用.为什么不呢?

                  I've been told not to use for...in with arrays in JavaScript. Why not?

                  推荐答案

                  原因是一个构造:

                  var a = []; // Create a new empty array.
                  a[5] = 5;   // Perfectly legal JavaScript that resizes the array.
                  
                  for (var i = 0; i < a.length; i++) {
                      // Iterate over numeric indexes from 0 to 5, as everyone expects.
                      console.log(a[i]);
                  }
                  
                  /* Will display:
                     undefined
                     undefined
                     undefined
                     undefined
                     undefined
                     5
                  */

                  有时可能完全不同:

                  var a = [];
                  a[5] = 5;
                  for (var x in a) {
                      // Shows only the explicitly set index of "5", and ignores 0-4
                      console.log(x);
                  }
                  
                  /* Will display:
                     5
                  */

                  还要考虑 JavaScript 库可能会做这样的事情,这会影响您创建的任何数组:

                  Also consider that JavaScript libraries might do things like this, which will affect any array you create:

                  // Somewhere deep in your JavaScript library...
                  Array.prototype.foo = 1;
                  
                  // Now you have no idea what the below code will do.
                  var a = [1, 2, 3, 4, 5];
                  for (var x in a){
                      // Now foo is a part of EVERY array and 
                      // will show up here as a value of 'x'.
                      console.log(x);
                  }
                  
                  /* Will display:
                     0
                     1
                     2
                     3
                     4
                     foo
                  */

                  这篇关于为什么使用“for...in"?数组迭代是个坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                  How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)
                    • <i id='kxBbs'><tr id='kxBbs'><dt id='kxBbs'><q id='kxBbs'><span id='kxBbs'><b id='kxBbs'><form id='kxBbs'><ins id='kxBbs'></ins><ul id='kxBbs'></ul><sub id='kxBbs'></sub></form><legend id='kxBbs'></legend><bdo id='kxBbs'><pre id='kxBbs'><center id='kxBbs'></center></pre></bdo></b><th id='kxBbs'></th></span></q></dt></tr></i><div id='kxBbs'><tfoot id='kxBbs'></tfoot><dl id='kxBbs'><fieldset id='kxBbs'></fieldset></dl></div>

                          <tfoot id='kxBbs'></tfoot>
                            <bdo id='kxBbs'></bdo><ul id='kxBbs'></ul>
                              <tbody id='kxBbs'></tbody>

                            <small id='kxBbs'></small><noframes id='kxBbs'>

                            <legend id='kxBbs'><style id='kxBbs'><dir id='kxBbs'><q id='kxBbs'></q></dir></style></legend>