diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
commit | 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch) | |
tree | a9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/zh-tw/web/javascript/reference/statements/break | |
parent | 074785cea106179cb3305637055ab0a009ca74f2 (diff) | |
download | translated-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/break')
-rw-r--r-- | files/zh-tw/web/javascript/reference/statements/break/index.html | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/statements/break/index.html b/files/zh-tw/web/javascript/reference/statements/break/index.html new file mode 100644 index 0000000000..ff72f9d25b --- /dev/null +++ b/files/zh-tw/web/javascript/reference/statements/break/index.html @@ -0,0 +1,120 @@ +--- +title: break +slug: Web/JavaScript/Reference/Statements/break +tags: + - JavaScript + - Statement +translation_of: Web/JavaScript/Reference/Statements/break +--- +<p>{{jsSidebar("Statements")}}</p> + +<p><strong>break 陳述句</strong>會中斷目前的迭代、{{jsxref("Statements/switch", "switch")}} 或 {{jsxref("Statements/label", "label")}} 陳述句,並將程式流程轉到被中斷之陳述句後的陳述句。</p> + +<div>{{EmbedInteractiveExample("pages/js/statement-break.html")}}</div> + + + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox"><code>break [<em>label</em>];</code></pre> + +<dl> + <dt><code>label</code></dt> + <dd>可選的。欲中斷陳述句的標籤 (label) 識別。若不是要中斷迭代或 {{jsxref("Statements/switch", "switch")}},則需加此參數。</dd> +</dl> + +<h2 id="說明">說明</h2> + +<p>中斷陳述 <code>break</code> 可加上標籤 (label) 參數,使其跳出被標籤的陳述語句。此中斷陳述 <code>break</code> 必須被包含在被標籤的陳述語句中。被標籤的陳述語句可被添加於任一個區塊 (<a class="internal" href="/en-US/docs/JavaScript/Reference/Statements/block" title="JavaScript/Reference/Statements/Block">block</a>) 前,而非限定在迴圈陳述。</p> + +<h2 id="範例">範例</h2> + +<p>下面函式包含一個中斷陳述 <code>break</code> ,當 <code>i</code> 值為 3 時,中斷 <code>while</code> 迴圈,並回傳 <code>3 * x</code> 。</p> + +<pre class="brush:js;highlight:[6];">function testBreak(x) { + var i = 0; + + while (i < 6) { + if (i == 3) { + break; + } + i += 1; + } + + return i * x; +}</pre> + +<p>The following code uses <code>break</code> statements with labeled blocks. A <code>break</code> statement must be nested within any label it references. Notice that <code>inner_block</code> is nested within <code>outer_block</code>.</p> + +<pre class="brush:js;highlight:[1,2,4];">outer_block: { + inner_block: { + console.log('1'); + break outer_block; // breaks out of both inner_block and outer_block + console.log(':-('); // skipped + } + console.log('2'); // skipped +} +</pre> + +<p>The following code also uses <code>break</code> statements with labeled blocks but generates a Syntax Error because its <code>break</code> statement is within <code>block_1</code> but references <code>block_2</code>. A <code>break</code> statement must always be nested within any label it references.</p> + +<pre class="brush:js;highlight:[1,3,6];">block_1: { + console.log('1'); + break block_2; // SyntaxError: label not found +} + +block_2: { + console.log('2'); +} +</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('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Initial definition. Unlabeled version.</td> + </tr> + <tr> + <td>{{SpecName('ES3')}}</td> + <td>{{Spec2('ES3')}}</td> + <td>Labeled version added.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-12.8', 'Break statement')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-break-statement', 'Break statement')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-break-statement', 'Break statement')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + + + +<p>{{Compat("javascript.statements.break")}}</p> + +<h2 id="參見">參見</h2> + +<ul> + <li>{{jsxref("Statements/continue", "continue")}}</li> + <li>{{jsxref("Statements/label", "label")}}</li> + <li>{{jsxref("Statements/switch", "switch")}}</li> +</ul> |