标记语句可以和 continue
语句一起使用。标记就是在一条语句前面加个可以引用的标识符(identifier)。
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.
备注:使用标记的循环或语句块非常罕见。通常情况下,可以使用函数调用而不是(基于标记的)循环跳转。
语法
label : statement
-
label
- 任何不属于保留关键字的 JavaScript 标识符。
-
statement
-
JavaScript 语句。
break
可用于任何标记语句,而continue
可用于循环标记语句。
描述
可使用一个标签来唯一标记一个循环,然后使用 break
或 continue
语句来指示程序是否中断循环或继续执行。
需要注意的是,JavaScript 没有 goto
语句,标记只能和 break
或 continue
一起使用。
在SyntaxError
(因为 let 是一个保留的标识符)。
示例
在 for
循环中使用带标记的 continue
语句
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"
使用带标记的 continue
语句
若给定一个数据数组和一个测试数组,则下面的例子会统计通过测试的数据的数量。
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++; }
在 for
循环中使用带标记的 break
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
使用带标记的 break
语句
若给定一个数据数组和一个测试数组,则下面的例子会判断是否所有数据均通过了测试。
var allPass = true; var i, j; top: for (i = 0; items.length; i++) for (j = 0; j < tests.length; i++) if (!tests[j].pass(items[i])){ allPass = false; break top; }
在标记块中使用 break
你可以在代码块中使用标记,但只有 break
语句可以使用非循环标记。
foo: { console.log('face'); break foo; console.log('this will not be executed'); } console.log('swap'); // this will log: // "face" // "swap
标记函数声明
从ECMAScript 2015开始,标准的函数声明现在对规范的 Web 兼容性附件中的非严格代码进行了标准化。
L: function F() {}
'use strict'; L: function F() {} // SyntaxError: functions cannot be labelled
无论是否处于严格模式下,生成器函数都不能被标记:
L: function* F() {} // SyntaxError: generator functions cannot be labelled
规范
规范 | 状态 | 备注 |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2 |
ECMAScript 5.1 (ECMA-262) Labelled statement |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Labelled statement |
Standard | |
ECMAScript Latest Draft (ECMA-262) Labelled statement |
Draft |
浏览器兼容性
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out
https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
label |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support