简体   繁体   English

Javascript:使用“标签”时出现奇怪的语法错误

[英]Javascript: weird syntax error when using “label”

This code piece 此代码段

abc:
var i=0;
for (; i < 10; ++i)
    if (i == 8) break abc;

has runtime exception, saying 有运行时异常,说

SyntaxError: Undefined label 'abc'

If I remove the line of 如果我删除

var i=0;

Then it's OK. 那没关系

This is weird to me. 这对我来说很奇怪。 Does javascript requires any label, if used by "break"/"continue", definition is only available to the code block right following it, or else it's not accessible? javascript是否需要任何标签,如果由“ break” /“ continue”使用,则定义仅可用于紧随其后的代码块,否则无法访问? Thanks. 谢谢。

From MDN : MDN

The break statement needs to be nested within the referenced label. break语句需要嵌套在引用的标签内。

So, yes. 所以,是的。

Your label needs to be directly before the loop: 您的标签需要直接在循环之前:

var i=0;
abc:
for (; i < 10; ++i)
    if (i == 8) break abc;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM