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

查看:94
本文介绍了在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天全站免登陆