aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/statements/label/index.html
blob: b67148b3592cd5c3a033301c7d3a8fb1f78af74c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
---
title: label
slug: Web/JavaScript/Reference/Statements/label
tags:
  - JavaScript
  - Statement
  - 陳述式
translation_of: Web/JavaScript/Reference/Statements/label
---
<div>{{jsSidebar("Statements")}}</div>

<p><strong>標記陳述式</strong>可以和 {{jsxref("Statements/break", "break")}} 或 {{jsxref("Statements/continue", "continue")}} 語句一起使用。標記就是在一條陳述式前面加個可以引用的識別符號。</p>

<div>{{EmbedInteractiveExample("pages/js/statement-label.html")}}</div>



<div class="notecard note">
<p><strong>備註:</strong>標記的迴圈或程式碼區塊非常罕見。通常可以使用函式呼叫而不是使用迴圈跳轉。</p>
</div>

<h2 id="語法">語法</h2>

<pre class="syntaxbox"><em>label</em> :
   <em>statement</em>
</pre>

<dl>
 <dt><code>label</code></dt>
 <dd>任何不是保留字的 JavaScript 識別符號。</dd>
 <dt><code>statement</code></dt>
 <dd>一個 JavaScript 陳述式。<code>break</code> 可用於任何標記陳述式,而 <code>continue</code> 可用於循環標記陳述式。</dd>
</dl>

<h2 id="描述">描述</h2>

<p>可使用一個標籤來唯一標記一個循環,然後使用 <code>break</code> 或 <code>continue</code> 陳述式來指示程式是否中斷循環或繼續執行。</p>

<p>需要注意的是 JavaScript <strong>沒有</strong> <code>goto</code> 陳述式,標記只能和 <code>break</code> 或 <code>continue</code> 一起使用。</p>

<p><a href="https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Strict_mode">嚴格模式</a>中,你不能使用 “<code>let</code>” 作為標籤名稱。它會拋出一個<a href="https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError" title="SyntaxError 物件代表嘗試解析語法上不合法的程式碼的錯誤。"><code>SyntaxError</code></a>(let 是一個保留的識別符號)。</p>

<h2 id="範例">範例</h2>

<h3 id="在_for_迴圈中使用帶標記的_continue"><code>for</code> 迴圈中使用帶標記的 <code>continue</code> </h3>

<pre class="brush: js">var i, j;

loop1:
for (i = 0; i &lt; 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (j = 0; j &lt; 3; j++) {   //The second for statement is labeled "loop2"
      if (i === 1 &amp;&amp; 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"
</pre>

<h3 id="使用帶標記的_continue_陳述式">使用帶標記的 <code>continue</code> 陳述式</h3>

<p>給定一組資料和一組測試,下面的例子可以統計通過測試的資料。</p>

<pre class="brush: js">var itemsPassed = 0;
var i, j;

top:
for (i = 0; i &lt; items.length; i++) {
  for (j = 0; j &lt; tests.length; j++) {
    if (!tests[j].pass(items[i])) {
      continue top;
    }
  }

  itemsPassed++;
}</pre>

<h3 id="在_for_迴圈中使用帶標記的_break"><code>for</code> 迴圈中使用帶標記的 <code>break</code></h3>

<pre class="brush: js">var i, j;

loop1:
for (i = 0; i &lt; 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (j = 0; j &lt; 3; j++) {   //The second for statement is labeled "loop2"
      if (i === 1 &amp;&amp; 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</pre>

<h3 id="使用帶標記_break_陳述式">使用帶標記 <code>break</code> 陳述式</h3>

<p>給定一組資料和一組測試,下面的例子判斷是否所有的資料均通過了測試。</p>

<pre class="brush: js">var allPass = true;
var i, j;

top:
for (i = 0; items.length; i++)
  for (j = 0; j &lt; tests.length; i++)
    if (!tests[j].pass(items[i])) {
      allPass = false;
      break top;
    }</pre>

<h3 id="在標記的區塊中使用_break">在標記的區塊中使用 <code>break</code></h3>

<p>你可以在程式碼區塊中使用標記,但只有 <code>break</code> 陳述式可以使用非迴圈的標記。</p>

<pre class="brush: js">foo: {
  console.log('face');
  break foo;
  console.log('this will not be executed');
}
console.log('swap');

// this will log:

// "face"
// "swap </pre>

<h3 id="標記的函式宣告式">標記的函式宣告式</h3>

<p>從 ECMAScript 2015 開始,標準的函式宣告式現在對規範的 <a href="http://www.ecma-international.org/ecma-262/6.0/#sec-labelled-function-declarations" rel="noopener">Web 相容性附件</a>中的非嚴格程式碼進行了標準化。</p>

<pre class="brush: js">L: function F() {}</pre>

<p><a href="https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Strict_mode">嚴格模式</a>中,這會拋出 {{jsxref("SyntaxError")}} 例外:</p>

<pre class="brush: js">'use strict';
L: function F() {}
// SyntaxError: functions cannot be labelled</pre>

<p><a href="https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Statements/function*">產生器函式</a>既不能在嚴格模式中標記,也不能在非嚴格模式中標記:</p>

<pre class="brush: js">L: function* F() {}
// SyntaxError: generator functions cannot be labelled
</pre>

<h2 id="規格">規格</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES3')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.2</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-12.12', 'Labelled statement')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-labelled-statements', 'Labelled statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-labelled-statements', 'Labelled statement')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="瀏覽器相容性">瀏覽器相容性</h2>



<p>{{Compat("javascript.statements.label")}}</p>

<h2 id="相關連結">相關連結</h2>

<ul>
 <li>{{jsxref("Statements/break", "break")}}</li>
 <li>{{jsxref("Statements/continue", "continue")}}</li>
</ul>