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

发布于 2024-10-16 03:58:24 字数 322 浏览 6 评论 0原文

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

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

doThis: //Label
doIt();

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

在我看来,这类似于其他语言中的 GOTO 语句,并且被认为是不好的做法。我的假设正确吗?

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

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 GOTO statement in other languages and would be considered bad practice. Would I be right in assuming this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猫九 2024-10-23 03:58:24

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

    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
    }

如果您使用不带“外部”标签的继续,它将转到内循环的下一次迭代。这就是 Javascript 中需要标签的原因。

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:

    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.

心碎无痕… 2024-10-23 03:58:24

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

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.

意中人 2024-10-23 03:58:24

2020年编辑,
根据MDN:

带标签的循环或块非常罕见。通常,函数调用
可以用来代替循环跳转。

我2015年的回答:

避免使用标签

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

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

2020 edit,
according to MDN:

Labelled loops or blocks are very uncommon. Usually, function calls
can be used instead of loop jumps.

My 2015 answer:

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.

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

芸娘子的小脾气 2024-10-23 03:58:24

带标签的中断可以打破任何代码块,而不仅仅是循环

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

结果:

测试1通过

测试2通过

测试3通过

labelled breaks can break out of any block 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

test 2 passed

test 3 passed

才能让你更想念 2024-10-23 03:58:24

有一种解决方法可以使用 while 循环代替内部 for 循环,以获得更好的可读性。

There is a workaround to use while loops instead of inner for loops for better readability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文