在 JavaScript 中使用标签是不好的做法吗? [英] Is using labels in JavaScript bad practice?

查看:28
本文介绍了在 JavaScript 中使用标签是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

推荐答案

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

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.

这篇关于在 JavaScript 中使用标签是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆