非法的继续声明?

2022-01-24 00:00:00 javascript coffeescript

我有类似下面的代码:

_.each data, (value, type) ->
    switch type
        when "answered"
            icon = "results_answered"
            label = definitions.results.metrics.ta
        when "leftblank"
            icon = "results_lb"
            label = definitions.results.metrics.tlb
        when "standing"
            icon = "results_standing"
            label = definitions.results.metrics.cs
        when "minimum"
            icon = "results_min"
            label = definitions.results.metrics.lowest
        else
            continue

    metricLine = utilities.div("metricline")
    metricLine.grab utilities.sprite(icon, "metric_icon")
    metricLine.grab utilities.div("metriclabel", label + ":")
    metricLine.grab utilities.div("metricvalue", value)
    metricContainer.grab(metricLine)

metricContainer

但它会向我的浏览器抛出以下错误:

But it throws the following error to my browser:

Uncaught SyntaxError: Illegal continue statement

Uncaught SyntaxError: Illegal continue statement

是否可以像我尝试的那样包含 continue 而不会引发错误?

Is it possible to include a continue like I am trying without throwing the error?

推荐答案

如果你想继续下一个循环迭代,你想要return,而不是continue,如你传递给 each 的是一个函数.

If you want to continue with the next loop iteration, you want return, not continue, as what you're passing to each is a function.

在您提到熟悉 C# foreach 循环的评论中,因此想要使用 continue.不同之处在于,使用 C# 的 foreach,您处理的是一个实际的循环构造,而 each 实际上是为每个循环迭代调用一个函数,所以它不是(在语言级别)一个循环,所以你不能继续它.

In a comment you mentioned being familiar wih the C# foreach loop, hence wanting to use continue. The difference is that wih C#'s foreach, you're dealing with an actual loop construct, whereas each is actually calling a function for each loop iteration, so it's not (at a language level) a loop, so you can't continue it.

相关文章