diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/statements/break | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/statements/break')
-rw-r--r-- | files/zh-cn/web/javascript/reference/statements/break/index.html | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/statements/break/index.html b/files/zh-cn/web/javascript/reference/statements/break/index.html new file mode 100644 index 0000000000..9cecd6eef9 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/statements/break/index.html @@ -0,0 +1,175 @@ +--- +title: break +slug: Web/JavaScript/Reference/Statements/break +tags: + - JavaScript + - Statement +translation_of: Web/JavaScript/Reference/Statements/break +--- +<div> +<div>{{jsSidebar("Statements")}}</div> +</div> + +<p><strong>break 语句</strong>中止当前循环,{{jsxref("Statements/switch", "switch")}}语句或{{jsxref("Statements/label", "label")}} 语句,并把程序控制流转到紧接着被中止语句后面的语句。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><code>break [label];</code></pre> + +<dl> + <dt><code>label</code> {{可选}}</dt> + <dd>与语句标签相关联的标识符。如果 break 语句不在一个循环或 {{jsxref("Statements/switch", "switch")}} 语句中,则该项是必须的。</dd> +</dl> + +<h2 id="描述">描述</h2> + +<p><code>break</code>语句包含一个可选的标签,可允许程序摆脱一个被标记的语句。<code>break</code>语句需要内嵌在引用的标签中。被标记的语句可以是任何 {{jsxref("Statements/block", "块")}}语句;不一定是循环语句。</p> + +<p>break语句不能在function函数体中直接使用,break语句应嵌套在要中断的当前循环、switch或label语句中。</p> + +<h2 id="示例">示例</h2> + +<h3 id="break_in_while_loop">break in while loop</h3> + +<p>下面的函数里有个 <code>break</code> 语句,当 <code>i</code> 为 3 时,会中止 {{jsxref("Statements/while", "while")}} 循环,然后返回 3 * <code>x</code> 的值。</p> + +<pre class="brush: js notranslate"><code class="language-js"><span class="keyword token">function</span> <span class="function token">testBreak</span><span class="punctuation token">(</span>x<span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">var</span> i <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">;</span> + + <span class="keyword token">while</span> <span class="punctuation token">(</span>i <span class="operator token"><</span> <span class="number token">6</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">if</span> <span class="punctuation token">(</span>i <span class="operator token">==</span> <span class="number token">3</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">break</span><span class="punctuation token">;</span> + <span class="punctuation token">}</span> + i <span class="operator token">+</span><span class="operator token">=</span> <span class="number token">1</span><span class="punctuation token">;</span> + <span class="punctuation token">}</span> + + <span class="keyword token">return</span> i <span class="operator token">*</span> x<span class="punctuation token">;</span> +<span class="punctuation token">}</span></code></pre> + +<h3 id="break_in_switch_statements">break in switch statements</h3> + +<p>在下面的代码中, <code>break</code> 使用在 {{jsxref("Statements/switch", "switch")}} 语句中,当遇到匹配到case后,就会执行相应的代码并中断循环体。</p> + +<pre class="notranslate">const food = "sushi"; + +switch (food) { + case "sushi": + console.log("Sushi is originally from Japan."); + break; + case "pizza": + console.log("Pizza is originally from Italy."); + break; + default: + console.log("I have never heard of that dish."); + break; +}</pre> + +<h3 id="break_in_labeled_blocks">break in labeled blocks</h3> + +<p>下面的代码中一起使用 <code>break</code> 语句和被标记的块语句。一个 <code>break</code> 语句必须内嵌在它引用的标记中。注意,<code>inner_block</code> 内嵌在 <code>outer_block</code> 中。</p> + +<pre class="brush:js;highlight:[1,3,5]; notranslate">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> + +<h3 id="break_in_labeled_blocks_that_throw">break in labeled blocks that throw</h3> + +<p>下面的代码同样使用了 <code>break</code> 语句和被标记的块语句,但是产生了一个语法错误,因为它的 <code>break</code> 语句在 <code>block_1</code> 中,但是引用了 <code>block_2</code>。<code>break</code> 语句必须内嵌在它引用的标签中。</p> + +<pre class="brush:js;highlight:[1,3,6]; notranslate">block_1:{ + console.log ('1'); + break block_2; // SyntaxError: label not found +} + +block_2:{ + console.log ('2'); +} +</pre> + +<h3 id="break_within_functions">break within functions</h3> + +<p>在下面的代码同样会产生SyntaxError,因为它并没被正确的使用在循环、switch或label语句中。</p> + +<pre class="notranslate">function testBreak(x) { + var i = 0; + + while (i < 6) { + if (i == 3) { + (function() { + break; + })(); + } + i += 1; + } + +return i * x; +} + +testBreak(1); // SyntaxError: Illegal break statement +</pre> + +<pre class="notranslate">block_1: { + console.log('1'); + ( function() { + break block_1; // SyntaxError: Undefined label 'block_1' + })(); +}</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> + +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div> + +<p>{{Compat("javascript.statements.break")}}</p> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("Statements/continue", "continue")}}</li> + <li>{{jsxref("Statements/label", "label")}}</li> + <li>{{jsxref("Statements/switch", "switch")}}</li> +</ul> |