この記事は翻訳が完了していません。 この記事の翻訳にご協力ください
labeled 文 は、break 文や continue 文と組み合わせて使用できます。これは文に、break または continue 文を使って参照できる識別子を与えます。
Note: Labeled loops or blocks are very uncommon. Usually function calls can be used instead of loop jumps.
構文
label : statement
label- 予約語ではない任意の JavaScript の識別子。
statement- 文。
breakは任意のラベル付き文で使うことができ、continueはループのラベル付き文で使うことができます。
説明
ループを識別するためにラベルを使い、そして、プログラムがループを中断すべきか、またはその実行を継続すべきかを指し示すために、break または continue 文を使うことができます。
JavaScript には goto 文がなく、ラベルと break または 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
for ループでラベル付き continue を使用する
var i, j;
loop1:
for (i = 0; i < 3; i++) { // 1 番目の for 文に "loop1" というラベルをつける
loop2:
for (j = 0; j < 3; j++) { // 2 番目の for 文に "loop2" というラベルをつける
if (i === 1 && j === 1) {
continue loop1;
}
console.log("i = " + i + ", j = " + j);
}
}
// 結果:
// "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"
// "i = 1, j = 1" と "i = 1, j = 2" をスキップしていることに注目
ラベル付き continue 文を使用する
配列 items と tests について、このサンプルはすべてを tests に渡した items の数を数えます。
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++) { // 1 番目の for 文に "loop1" というラベルをつける
loop2:
for (j = 0; j < 3; j++) { // 2 番目の for 文に "loop2" というラベルをつける
if (i === 1 && j === 1) {
break loop1;
}
console.log("i = " + i + ", j = " + j);
}
}
// 結果:
// "i = 0, j = 0"
// "i = 0, j = 1"
// "i = 0, j = 2"
// "i = 1, j = 0"
// continue の例との違いに注目
ラベル付き break 文を使用する
配列 items と tests について、このサンプルは items のすべてを 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;
}
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
仕様
| 仕様書 | 策定状況 | コメント |
|---|---|---|
| ECMAScript 3rd Edition (ECMA-262) | 標準 | 最初期の定義。JavaScript 1.2 で実装。 |
| ECMAScript 5.1 (ECMA-262) Labelled statement の定義 |
標準 | |
| ECMAScript 2015 (6th Edition, ECMA-262) Labelled statement の定義 |
標準 | |
| ECMAScript (ECMA-262) Labelled statement の定義 |
現行の標準 |
ブラウザ実装状況
| デスクトップ | モバイル | サーバー | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
label | Chrome 完全対応 1 | Edge 完全対応 12 | Firefox 完全対応 1 | IE 完全対応 4 | Opera 完全対応 4 | Safari 完全対応 1 | WebView Android 完全対応 1 | Chrome Android 完全対応 18 | Firefox Android 完全対応 4 | Opera Android 完全対応 10.1 | Safari iOS 完全対応 1 | Samsung Internet Android 完全対応 1.0 | nodejs 完全対応 0.1.100 |
凡例
- 完全対応
- 完全対応