【问题标题】:How do I build a loop in JavaScript? [closed]如何在 JavaScript 中构建循环? [关闭]
【发布时间】:2010-09-08 07:13:36
【问题描述】:

如何在 JavaScript 中构建循环?

【问题讨论】:

    标签: javascript loops


    【解决方案1】:

    JavaScript 中的循环如下所示:

    for (var = startvalue; var <= endvalue; var = var + increment) {
        // code to be executed
    }
    

    【讨论】:

      【解决方案2】:

      这里是一个 for 循环的例子:

      我们有一个项目数组节点

      for(var i = 0; i< nodes.length; i++){
          var node = nodes[i];
          alert(node);
      }
      

      【讨论】:

        【解决方案3】:

        For 循环

        for (i = startValue; i <= endValue; i++) {
            // Before the loop: i is set to startValue
            // After each iteration of the loop: i++ is executed
            // The loop continues as long as i <= endValue is true
        }
        

        For...in 循环

        for (i in things) {
            // If things is an array, i will usually contain the array keys *not advised*
            // If things is an object, i will contain the member names
            // Either way, access values using: things[i]
        }
        

        使用for...in 循环遍历数组是不好的做法。它违反了ECMA 262 标准,并且在将非标准属性或方法添加到 Array 对象时可能会导致问题,例如通过Prototype(感谢 Chase Seibert 在 cmets 中指出这一点)

        While 循环

        while (myCondition) {
            // The loop will continue until myCondition is false
        }
        

        【讨论】:

        • 你不应该使用 for...in 来循环数组。这将导致原型出现问题。见prototypejs.org/api/array
        • 如果检查 hasOwnProperty,可以避免 for-in 循环的问题: if(!things.hasOwnProperty(i)) { continue; }
        【解决方案4】:

        您也可以考虑优化循环速度;见http://www.robertnyman.com/2008/04/11/javascript-loop-performance/

        【讨论】:

          【解决方案5】:

          除了内置循环(while() ...do ... while()for() ...)之外,还有一个自调用函数的结构,也称为递归来创建一个没有三个内置循环结构。

          考虑以下几点:

          // set the initial value
          var loopCounter = 3;
          
          // the body of the loop
          function loop() {
          
              // this is only to show something, done in the loop
              document.write(loopCounter + '<br>');
          
              // decrease the loopCounter, to prevent running forever
              loopCounter--;
          
              // test loopCounter and if truthy call loop() again 
              loopCounter && loop();
          }
          
          // invoke the loop
          loop();

          这个结构不用说,经常和返回值结合使用,所以这是一个小例子,如何处理不是在第一次可用,而是在递归结束时的值:

          function f(n) {
              // return values for 3 to 1
              //       n   -n  ~-n   !~-n   +!~-n   return
              // conv int neg bitnot  not  number 
              //       3   -3   2    false    0    3 * f(2)
              //       2   -2   1    false    0    2 * f(1)
              //       1   -1   0     true    1        1
              // so it takes a positive integer and do some conversion like changed sign, apply
              // bitwise not, do logical not and cast it to number. if this value is then
              // truthy, then return the value. if not, then return the product of the given
              // value and the return value of the call with the decreased number
              return +!~-n || n * f(n - 1);
          }
          
          document.write(f(7));

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-11-18
            • 1970-01-01
            • 2015-04-15
            • 2011-07-30
            相关资源
            最近更新 更多