aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/statements/break/index.html
blob: 9cecd6eef90a805dc7eb68e0c70c12d0f5462882 (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
---
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">&lt;</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 &lt; 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>