语法错误:未找到标签

breakcontinue 语句引用的标签在包含 breakcontinue 语句的任何语句中都不存在时,会发生 JavaScript 异常 "未找到标签"。

¥The JavaScript exception "label not found" occurs when a break or continue statement references a label that does not exist on any statement that contains the break or continue statement.

信息

¥Message

SyntaxError: Undefined label 'label' (V8-based)
SyntaxError: label not found (Firefox)
SyntaxError: Cannot use the undeclared label 'label'. (Safari)

错误类型

¥Error type

SyntaxError

什么地方出了错?

¥What went wrong?

在 JavaScript 中,labels 非常有限:你只能将它们与 breakcontinue 语句一起使用,并且只能从标记语句中包含的语句跳转到它们。你无法从程序中的任何位置跳转到该标签。

¥In JavaScript, labels are very limited: you can only use them with break and continue statements, and you can only jump to them from a statement contained within the labeled statement. You cannot jump to this label from anywhere in the program.

示例

¥Examples

非语法跳转

¥Unsyntactic jump

你不能像使用 goto 一样使用标签。

¥You cannot use labels as if they are goto.

js
start: console.log("Hello, world!");
console.log("Do it again");
break start;

相反,你只能使用标签来增强 breakcontinue 语句的正常语义。

¥Instead, you can only use labels to enhance the normal semantics of break and continue statements.

js
start: {
  console.log("Hello, world!");
  if (Math.random() > 0.5) {
    break start;
  }
  console.log("Maybe I'm logged");
}

也可以看看

¥See also