label - JavaScript 编辑

The labeled statement can be used with break or continue statements. It is prefixing a statement with an identifier which you can refer to.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Note: Labeled loops or blocks are very uncommon. Usually function calls can be used instead of loop jumps.

Syntax

label :
  statement
label
Any JavaScript identifier that is not a reserved word.
statement
A JavaScript statement. break can be used with any labeled statement, and continue can be used with looping labeled statements.

Description

You can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.

Note that JavaScript has no goto statement, you can only use labels with break or continue.

In strict mode code, you can't use "let" as a label name. It will throw a SyntaxError (let is a reserved identifier).

Examples

Using a labeled continue with for loops

var i, j;

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

// Output is:
//   "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"
// Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2"

Using a labeled continue statement

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

var itemsPassed = 0;
var i, j;

top:
for (i = 0; i < items.length; i++) {
  for (j = 0; j < tests.length; j++) {
    if (!tests[j].pass(items[i])) {
      continue top;
    }
  }

  itemsPassed++;
}

Using a labeled break with for loops

var i, j;

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

// Output is:
//   "i = 0, j = 0"
//   "i = 0, j = 1"
//   "i = 0, j = 2"
//   "i = 1, j = 0"
// Notice the difference with the previous continue example

Using a labeled break statement

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

var allPass = true;
var i, j;

top:
for (i = 0; i < items.length; i++) {
  for (j = 0; j < tests.length; j++) {
    if (!tests[j].pass(items[i])) {
      allPass = false;
      break top;
    }
  }
}

Using a labeled block with break

You can use labels within simple blocks, but only break statements can make use of non-loop labels.

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

// this will log:

// "face"
// "swap" 

Labeled function declarations

Starting with ECMAScript 2015, labeled function declarations are now standardized for non-strict code in the web compatibility annex of the specification.

L: function F() {}

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

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

Generator functions can neither be labeled in strict code, nor in non-strict code:

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

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'Labelled statement' in that specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:142 次

字数:8117

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文