• <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"?数组迭代是个坏主意?

        时间:2024-04-18

          <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"?数组迭代是个坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将 WebGL 非连续行渲染为单个对象 下一篇:在 JavaScript 中对数组进行 for-each

                  相关文章

                2. <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>

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

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