【问题标题】:Equivalent of a continue statement in JavaScript等价于 JavaScript 中的 continue 语句
【发布时间】:2016-09-17 08:26:41
【问题描述】:

我想知道 JavaScript 中 continue 语句的结构化等价物是什么?我试图摆脱 continue 声明,但不知道如何。谁能指出我正确的方向?谢谢!

function Hand() {
    this.cards = new Array();

    this.addOneCard = function(card) {
        this.cards.push(card);
    }

    this.evaluateHand = function(){
        // Needs to handle two aces better
        var total1 = new Number;
        var total2 = new Number;

        for(var i in this.cards) {
            if (this.cards[i].value == "A") { 
                total1 += 1;
                total2 += 11;
                continue;
            }
            if (isNaN(this.cards[i].value * 1)) {
                total1 += 10;
                total2 += 10;
                continue;
            } 
            total1 += Number(this.cards[i].value);
            total2 += Number(this.cards[i].value);
        }
        return [total1, total2];
    };
}

【问题讨论】:

  • 为什么需要改变? continue 是语言的一部分,非常适合您的需求。

标签: javascript


【解决方案1】:

continue 语句在 JavaScript 中有效。您可以像在任何语言中一样使用它。

说完之后,您可以阅读此interesting discussion,了解您为什么要避免它以及如何避免它。

【讨论】:

    【解决方案2】:

    else if 对在这里为您提供帮助:

        for(var i in this.cards) {
            if (this.cards[i].value == "A") { 
                total1 += 1;
                total2 += 11;
            }
            else if (isNaN(this.cards[i].value * 1)) {
                total1 += 10;
                total2 += 10;
            } 
            else { 
                total1 += Number(this.cards[i].value);
                total2 += Number(this.cards[i].value);
            }
        }
    

    【讨论】:

      【解决方案3】:

      这应该适用于任何语言,而不仅仅是 java 脚本

      function Hand() {
          this.cards = new Array();
      
          this.addOneCard = function(card) {
              this.cards.push(card);
          }
      
          this.evaluateHand = function(){
              // Needs to handle two aces better
              var total1 = new Number;
              var total2 = new Number;
      
              for(var i in this.cards) {
                  if (this.cards[i].value == "A") { 
                      total1 += 1;
                      total2 += 11;
                  }
                  else if (isNaN(this.cards[i].value * 1)) {
                      total1 += 10;
                      total2 += 10;
                  }
                  else {
                      total1 += Number(this.cards[i].value);
                      total2 += Number(this.cards[i].value);
                  }
              }
              return [total1, total2];
          };
      }
      

      【讨论】:

        【解决方案4】:

        一个选项是:

        this.cards.forEach(function(card) {
            if (card.value == "A") { 
                total1 += 1;
                total2 += 11;
                return;
            }
            if (isNaN(card.value * 1)) {
                total1 += 10;
                total2 += 10;
                return;
            } 
            total1 += Number(card.value);
            total2 += Number(card.value);
        });
        

        显然有些人认为return 会终止一切...return 会停止为当前元素运行的函数,从而立即开始下一次迭代,就像continue 一样。我并不是说这比仅使用 continue 更好,但它绝对是一个替代方案。

        【讨论】:

        • return 结束函数,但 continue 继续 for 循环。
        • returns 结束循环中的函数并开始循环的下一次迭代。这不就是continue 在做什么吗?
        • 不,return 终止函数(和)。没有更多的迭代。 continue 跳转到 for 循环的开头并跳过部分到 for 循环的末尾。
        • 这是不正确的。我建议您打开控制台并运行类似var a = []; [1, 2, 3, 4].forEach(function(i) { if (i % 2 == 0) { return; } a.push(i); } 的内容,然后查看a 中的内容(提示:它是[1, 3]
        • forEach()return 当然等同于 forcontinue,但我的印象是 OP 希望避免像那样跳出中间。 (不确定这是否是 OP 的目标,但我知道有些人认为函数的结构应该在末尾有一个返回,并且没有像 continuebreak 这样的跳转语句 - 我不同意那些人。)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多