在 JavaScript 中使用标签是不好的做法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4906762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:40:57  来源:igfitidea点击:

Is using labels in JavaScript bad practice?

javascriptloopslabelnested-loopsbreak

提问by Ryan

I just found out about using label s in JavaScript, such as:

我刚刚发现在 JavaScript 中使用 label ,例如:

for (var i in team) {
    if(i === "something") {
        break doThis: //Goto the label
    } else {
        doThat();
    }
}

doThis: //Label
doIt();

I've not heard about this until now and I can't find much information online about it and I'm beginning to think there is a reason for that.

直到现在我还没有听说过这个,我在网上找不到很多关于它的信息,我开始认为这是有原因的。

It seems to me like this is similar to a GOTOstatement in other languages and would be considered bad practice. Would I be right in assuming this?

在我看来,这类似于GOTO其他语言中的声明,会被认为是不好的做法。我的假设是否正确?

采纳答案by Sarfraz

Those are loop breaker identifiers. They are useful if you have nested loops (loops inside loops) and using these identifiers, you can conditionally specify when and which loop to break out from.

这些是环路断路器标识符。如果您有嵌套循环(循环内的循环)并使用这些标识符,则它们很有用,您可以有条件地指定何时以及从哪个循环中退出。

回答by tdobek

The labels in JavaScript are used mainly with break, or continue in nested loops to be able to break the outer, or continue the outer loop from the code inside inner loop:

JavaScript 中的标签主要与 break 或 continue 在嵌套循环中使用,以便能够中断外部循环,或从内部循环内部的代码继续外部循环:

    outer:
    for (let i = 0; i < 10; i++)
    { 
       let k = 5;
       for (let j = 0; j < 10; j++) // inner loop
          if (j > 5) 
               break; // inner 
          else
               continue outer;  // it will go to next iteration of outer loop
    }

If you used continue without 'outer' label, it would go to the next iteration of inner loop. That's why there is a need for labels in Javascript.

如果您使用不带“外部”标签的 continue,它将进入内部循环的下一次迭代。这就是为什么在 Javascript 中需要标签的原因。

回答by Gal Margalit

Avoid using labels

Labels are not very commonly used in JavaScript since they make programs harder to read and understand. As much as possible, avoid using labels and, depending on the cases, prefer calling functions or throwing an error.

避免使用标签

标签在 JavaScript 中不是很常用,因为它们使程序更难阅读和理解。尽可能避免使用标签,并根据情况选择调用函数或抛出错误。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

回答by Pall Arpad

labelled breaks can break out of anyblock of code not just loops

标记中断可以跳出任何代码块,而不仅仅是循环

<p id="test1"></p>
<p id="test2"></p>
<p id="test3"></p>
<p id="test4"></p>
<p id="test5"></p>

test: {                            
    document.getElementById('test1').innerHTML = "test 1 passed";
    document.getElementById('test2').innerHTML = "test 2 passed";
    document.getElementById('test3').innerHTML = "test 3 passed";
    break test;
    document.getElementById('test4').innerHTML = "test 4 passed";
    document.getElementById('test5').innerHTML = "test 5 passed";
}

result:

结果:

test 1 passed

测试 1 通过

test 2 passed

测试 2 通过

test 3 passed

测试 3 通过