aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/statements/label/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
commit218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch)
treea9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/zh-tw/web/javascript/reference/statements/label/index.html
parent074785cea106179cb3305637055ab0a009ca74f2 (diff)
downloadtranslated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip
initial commit
Diffstat (limited to 'files/zh-tw/web/javascript/reference/statements/label/index.html')
-rw-r--r--files/zh-tw/web/javascript/reference/statements/label/index.html203
1 files changed, 203 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/statements/label/index.html b/files/zh-tw/web/javascript/reference/statements/label/index.html
new file mode 100644
index 0000000000..4939b7b95f
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/statements/label/index.html
@@ -0,0 +1,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="note">
+<p>標記的迴圈或程式碼區塊非常罕見。通常可以使用函式呼叫而不是使用迴圈跳轉。</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> 迴圈中使用帶標記的 <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(255, 255, 255, 0.4);">break</span></font> </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>