标签声明

带标签的语句是任何以标识符为前缀的 statement。你可以使用嵌套在带标签语句中的 breakcontinue 语句跳转到该标签。

¥A labeled statement is any statement that is prefixed with an identifier. You can jump to this label using a break or continue statement nested within the labeled statement.

Try it

语法

¥Syntax

js
label:
  statement
label

任何不是 保留字 的 JavaScript identifier

statement

JavaScript 语句。break 可以在任何标记语句中使用,continue 可以在标记循环语句中使用。

描述

¥Description

你可以使用标签来标识语句,然后使用 breakcontinue 语句引用它。请注意,JavaScript 没有 goto 语句;你只能使用带有 breakcontinue 的标签。

¥You can use a label to identify a statement, and later refer to it using a break or continue statement. Note that JavaScript has no goto statement; you can only use labels with break or continue.

任何引用 labelbreakcontinue 都必须包含在由 label 标记的 statement 中。将 label 视为仅在 statement 范围内可用的变量。

¥Any break or continue that references label must be contained within the statement that's labeled by label. Think about label as a variable that's only available in the scope of statement.

如果执行 statement 时遇到 break label; 语句,则 statement 的执行终止,并继续执行紧随标记语句之后的语句。

¥If a break label; statement is encountered when executing statement, execution of statement terminates, and execution continues at the statement immediately following the labeled statement.

仅当 statement循环语句 之一时,才能使用 continue label;。如果执行 statement 时遇到 continue label; 语句,则在循环的下一次迭代中继续执行 statement。没有标签的 continue; 只能继续最内层的循环,而 continue label; 允许继续任何给定的循环,即使该语句嵌套在其他循环中也是如此。

¥continue label; can only be used if statement is one of the looping statements. If a continue label; statement is encountered when executing statement, execution of statement continues at the next iteration of the loop. continue; without a label can only continue the innermost loop, while continue label; allows continuing any given loop even when the statement is nested within other loops.

一条语句可以有多个标签。在这种情况下,标签在功能上都是等效的。

¥A statement can have multiple labels. In this case, the labels are all functionally equivalent.

示例

¥Examples

使用带标签的 continue 和 for 循环

¥Using a labeled continue with for loops

js
// The first for statement is labeled "loop1"
loop1: for (let i = 0; i < 3; i++) {
  // The second for statement is labeled "loop2"
  loop2: for (let j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
      continue loop1;
    }
    console.log(`i = ${i}, j = ${j}`);
  }
}

// Logs:
// i = 0, j = 0
// i = 0, j = 1
// i = 0, j = 2
// i = 1, j = 0
// i = 2, j = 0
// i = 2, j = 1
// i = 2, j = 2

请注意它如何跳过 "我 = 1,j = 1" 和 "我 = 1,j = 2"。

¥Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2".

在 for 循环中使用带标签的 break

¥Using a labeled break with for loops

js
let i, j;

// The first for statement is labeled "loop1"
loop1: for (i = 0; i < 3; i++) {
  // The second for statement is labeled "loop2"
  loop2: for (j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
      break loop1;
    }
    console.log(`i = ${i}, j = ${j}`);
  }
}

// Logs:
// i = 0, j = 0
// i = 0, j = 1
// i = 0, j = 2
// i = 1, j = 0

请注意与前面的 continue 示例的区别:当遇到 break loop1 时,终止外循环的执行,因此不再有 "我 = 1,j = 0" 之外的日志;当遇到 continue loop1 时,下一次迭代时继续执行外循环,因此只跳过 "我 = 1,j = 1" 和 "我 = 1,j = 2"。

¥Notice the difference with the previous continue example: when break loop1 is encountered, the execution of the outer loop is terminated, so there are no further logs beyond "i = 1, j = 0"; when continue loop1 is encountered, the execution of the outer loop continues at the next iteration, so only "i = 1, j = 1" and "i = 1, j = 2" are skipped.

使用带标签的 continue 语句

¥Using a labeled continue statement

给定一个项目数组和一个测试数组,此示例计算通过所有测试的项目数。

¥Given an array of items and an array of tests, this example counts the number of items that pass all the tests.

js
// Numbers from 1 to 100
const items = Array.from({ length: 100 }, (_, i) => i + 1);
const tests = [
  { pass: (item) => item % 2 === 0 },
  { pass: (item) => item % 3 === 0 },
  { pass: (item) => item % 5 === 0 },
];
let itemsPassed = 0;

itemIteration: for (const item of items) {
  for (const test of tests) {
    if (!test.pass(item)) {
      continue itemIteration;
    }
  }

  itemsPassed++;
}

请注意 continue itemIteration; 语句如何跳过当前项目的其余测试以及更新 itemsPassed 计数器的语句,并继续执行下一个项目。如果你不使用标签,则需要使用布尔标志。

¥Note how the continue itemIteration; statement skips the rest of the tests for the current item as well as the statement that updates the itemsPassed counter, and continues with the next item. If you don't use a label, you would need to use a boolean flag instead.

js
// Numbers from 1 to 100
const items = Array.from({ length: 100 }, (_, i) => i + 1);
const tests = [
  { pass: (item) => item % 2 === 0 },
  { pass: (item) => item % 3 === 0 },
  { pass: (item) => item % 5 === 0 },
];
let itemsPassed = 0;

for (const item of items) {
  let passed = true;
  for (const test of tests) {
    if (!test.pass(item)) {
      passed = false;
      break;
    }
  }
  if (passed) {
    itemsPassed++;
  }
}

使用带标签的 break 语句

¥Using a labeled break statement

给定一个项目数组和一组测试,此示例确定所有项目是否通过所有测试。

¥Given an array of items and an array of tests, this example determines whether all items pass all tests.

js
// Numbers from 1 to 100
const items = Array.from({ length: 100 }, (_, i) => i + 1);
const tests = [
  { pass: (item) => item % 2 === 0 },
  { pass: (item) => item % 3 === 0 },
  { pass: (item) => item % 5 === 0 },
];
let allPass = true;

itemIteration: for (const item of items) {
  for (const test of tests) {
    if (!test.pass(item)) {
      allPass = false;
      break itemIteration;
    }
  }
}

同样,如果你不使用标签,则需要使用布尔标志。

¥Again, if you don't use a label, you would need to use a boolean flag instead.

js
// Numbers from 1 to 100
const items = Array.from({ length: 100 }, (_, i) => i + 1);
const tests = [
  { pass: (item) => item % 2 === 0 },
  { pass: (item) => item % 3 === 0 },
  { pass: (item) => item % 5 === 0 },
];
let allPass = true;

for (const item of items) {
  let passed = true;
  for (const test of tests) {
    if (!test.pass(item)) {
      passed = false;
      break;
    }
  }
  if (!passed) {
    allPass = false;
    break;
  }
}

使用带中断的标记块

¥Using a labeled block with break

你可以标记循环以外的语句,例如简单块,但只有 break 语句可以引用非循环标签。

¥You can label statements other than loops, such as simple blocks, but only break statements can reference non-loop labels.

js
foo: {
  console.log("face");
  break foo;
  console.log("this will not be executed");
}
console.log("swap");

// Logs:
// "face"
// "swap"

带标签的函数声明

¥Labeled function declarations

标签只能应用于 声明,而不是声明。有一种遗留语法允许在非严格代码中标记函数声明:

¥Labels can only be applied to statements, not declarations. There is a legacy grammar that allows function declarations to be labeled in non-strict code:

js
L: function F() {}

然而,在 严格模式 代码中,这将抛出 SyntaxError

¥In strict mode code, however, this will throw a SyntaxError:

js
"use strict";
L: function F() {}
// SyntaxError: functions cannot be labelled

非普通函数,例如 生成器函数异步函数 既不能在严格代码中标记,也不能在非严格代码中标记:

¥Non-plain functions, such as generator functions and async functions can neither be labeled in strict code, nor in non-strict code:

js
L: function* F() {}
// SyntaxError: generator functions cannot be labelled

带标签的函数声明语法是 deprecated,你不应该使用它,即使在非严格代码中也是如此。你实际上无法在函数体内跳转到该标签。

¥The labeled function declaration syntax is deprecated and you should not use it, even in non-strict code. You cannot actually jump to this label within the function body.

规范

Specification
ECMAScript Language Specification
# sec-labelled-statements

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also