gpt4 book ai didi

javascript - JavaScript 中 "break Identifier"的用例是什么?

转载 作者:数据小太阳 更新时间:2023-10-29 06:10:45 24 4
gpt4 key购买 nike

规范 goes

BreakStatement :   
break ;
break [no LineTerminator here] Identifier ;

然后就可以了

The program contains a break statement with the optional Identifier, where Identifier does not appear in the label set of an enclosing (but not crossing function boundaries) Statement.

...

A BreakStatement with an Identifier is evaluated as follows:

Return (break, empty, Identifier).

这到底是什么意思?

最佳答案

标签是这样的:

// ...
mylabel:
// ...

这可以作为语句放在任何地方。

当有多个嵌套的 for 循环时,break/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"

Source .

关于javascript - JavaScript 中 "break Identifier"的用例是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34364814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com