简体   繁体   English

等效于JavaScript中的continue语句

[英]Equivalent of a continue statement in JavaScript

I was wondering what the structured equivalent of a continue statement is in JavaScript? 我想知道在JavaScript中,continue语句的结构等价物是什么? I am trying to get rid of a continue statement but not sure how. 我试图摆脱继续声明,但不知道如何。 Can anyone point me in the right direction? 谁能指出我正确的方向? Thanks! 谢谢!

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 statement is valid in JavaScript. continue语句在JavaScript中有效。 You can use it as you would in any language. 您可以像使用任何语言一样使用它。

After saying that, you can read this interesting discussion on why you might want to avoid it, and how. 在说完之后,你可以阅读这个有趣的讨论 ,为什么你可能想要避免它,以及如何。

else if pair will help you here: 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);
        }
    }

This should hold true for any language and not just java script 这适用于任何语言,而不仅仅是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];
    };
}

One option is: 一种选择是:

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);
});

Apparently some people think return terminates everything... return stops the function running for the current element, making the next iteration start right away, as would do continue . 显然有些人认为return终止所有内容... return停止为当前元素运行的函数,使下一次迭代立即开始,就像continue I'm not saying this is a better option than just using continue , but it's definitively an alternative. 我不是说这是一个比使用continue更好的选择,但它绝对是另一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM