label statement
来源:互联网 发布:星际争霸2和lol知乎 编辑:程序博客网 时间:2024/06/08 19:48
标记语句可以和 break
或 continue
语句一起使用。标记就是在一条语句前面加个可以引用的标识符。
语法
label : statement
label
- 任何不是保留关键字的 JavaScript 标识符。
statement
- 语句。
break
可用于任何标记语句,而continue
可用于循环标记语句。
描述
可使用一个标签来唯一标记一个循环,然后使用 break
或 continue
语句来指示程序是否中断循环或继续执行。
需要注意的是 JavaScript 没有 goto
语句,标记只能和 break
或 continue
一起使用。
在严格模式中,你不能使用“let
”作为标签名称。它会抛出一个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() {}
在严格模式中,这会抛出 SyntaxError
:
'use strict';L: function F() {}// SyntaxError: functions cannot be labelled
生成器函数既不能在严格模式中标记,也不能在非严格模式中标记:
L: function* F() {}// SyntaxError: generator functions cannot be labelled
规范
Labelled statementStandard ECMAScript 2015 (6th Edition, ECMA-262)
Labelled statementStandard ECMAScript Latest Draft (ECMA-262)
Labelled statementLiving Standard
浏览器兼容
- Desktop
- Mobile
阅读全文
0 0
- label statement
- javascript中的标签语句label:statement
- a label can only be part of a statement and a declaration is not a statement
- a label can only be part of a statement and a declaration is not a statement
- a label can only be part of a statement and a declaration is not a statement
- a label can only be part of a statement and a declaration is not a statement
- a label can only be part of a statement and a declaration is not a statement
- a label can only be part of a statement and a declaration is not a statement
- switch引发的错误 a label can only be part of a statement and a declaration is not a statement
- switch引发的错误:a label can only be part of a statement and a declaration is not a statement
- error: a label can only be part of a statement and a declaration is not a statement
- 错误“a label can only be part of a statement and a declaration is not a statement”解决方法
- 错误“a label can only be part of a statement and a declaration is not a statement”解决方法
- switch 错误:a label can only be part of a statement and a declaration is not a statement
- switch引发的错误 a label can only be part of a statement and a declaration is not a statement
- ERROR:a label can only be part of a statement and a declaration is not a statement
- 【C/C++】error: a label can only be part of a statement and a declaration is not a statement
- C语言:error: a label can only be part of a statement and a declaration is not a statement|
- 23种设计模式
- Ubuntu17.04安装wps
- $match
- 注解的定义以及获取注解里面的方法,变量
- 洛谷1137 旅行计划
- label statement
- Tarjan求割点
- [二分答案 DP检验 几何] BalkanOI 2017. Cruise
- 系统限制
- js日期格式化
- Sublime Text3自动保存的功能(失去焦点自动保存)
- solr7在tomcat上的部署
- [线段树 哈希] BalkanOI 2016. Haker
- CentOS 6.5 LVS+KeepAlived(DR模式)搭建高可用负载均衡集群