JavaScript 中的 Label 语句


简介

在javascript中,我们可能很少会去用到 Label 语句,但是熟练的应用 Label 语句,尤其是在嵌套循环中熟练应用 break, continue 与 Label 可以精确的返回到你想要的程序的位置。

Label 语句,按书本上说的语法是:

Label: statement

如:

begin: for (var i = 0; i < 10 ; i++ ){
    alert(i);
}

典型例子

举一个比较典型的例子,看完后即明白 Label 的应用:(未添加 Label)

不使用 Label

var num = 0;
for (var i = 0 ; i < 10 ; i++){
    for (var j = 0 ; j < 10 ; j++){
        if( i == 5 && j == 5 ){
            break;
        }
        num++;
    }
}
alert(num); // 循环在 i 为5,j 为5的时候跳出 j循环,但会继续执行 i 循环,输出 95

使用 Label

对比使用了 Label 之后的程序:(添加 Label 后)

var num = 0;
outPoint:
for (var i = 0 ; i < 10 ; i++){
    for (var j = 0 ; j < 10 ; j++){
        if( i == 5 && j == 5 ){
            break outPoint;
        }
        num++;
    }
}
alert(num); // 循环在 i 为5,j 为5的时候跳出双循环,返回到outPoint层继续执行,输出 55

mozilla

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

Syntax

label :
   statement

label

Any JavaScript identifier that is not a reserved word.

** statement**

Statements. 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.

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; items.length; i++)
  for (j = 0; j < tests.length; i++)
    if (!tests[j].pass(items[i])){
      allPass = false;
      break top;
    }

知识共享许可协议
《JavaScript 中的 Label 语句》 常伟华 创作。
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议 | 3.0 中国大陆许可协议进行许可。

站内公告

A PHP Error was encountered

Severity: Core Warning

Message: PHP Startup: zip: Unable to initialize module Module compiled with module API=20060613 PHP compiled with module API=20090626 These options need to match

Filename: Unknown

Line Number: 0

Backtrace: