aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/conflicting/web/javascript/reference/statements/switch/index.html
blob: 21d8c25aa3390da4a92dac6e509c21b0617adbf1 (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
---
title: default
slug: conflicting/Web/JavaScript/Reference/Statements/switch
tags:
  - JavaScript
  - Keyword
translation_of: Web/JavaScript/Reference/Statements/switch
translation_of_original: Web/JavaScript/Reference/Statements/default
original_slug: Web/JavaScript/Reference/Statements/default
---
<div>{{jsSidebar("Statements")}}</div>

<div></div>

<p><strong>default 关键字</strong>可以在 JavaScript 的两种情况下使用:在 {{jsxref("Statements/switch", "switch")}} ,或 {{jsxref("Statements/export", "export")}} 中。</p>

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

<p>{{jsxref("Statements/switch", "switch")}} 语句中使用:</p>

<pre class="syntaxbox">switch (expression) {
  case value1:
    //当表达式的值和value1匹配执行这里的语句
    [break;]
  default:
    //当表达式的值没有匹配,执行这里的语句
    [break;]
}</pre>

<p>{{jsxref("Statements/export", "export")}} 中使用:</p>

<pre class="syntaxbox">export default <em>nameN</em> </pre>

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

<p>更多细节,参见</p>

<ul>
 <li>{{jsxref("Statements/switch", "switch")}} 语句和</li>
 <li>{{jsxref("Statements/export", "export")}} 语句页面。</li>
</ul>

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

<h3 id="在switch语句中使用default"><code>switch</code>语句中使用<code>default</code></h3>

<p>在以下示例中,如果<code>expr</code>为“Oranges”或“Apples”,程序将匹配“Oranges”或“Apples”的值并执行相应的声明。在任何其它情况下,<code>default</code>关键字将执行关联的语句。</p>

<pre class="brush: js">switch (expr) {
  case "Oranges":
    console.log("Oranges are $0.59 a pound.");
    break;
  case "Apples":
    console.log("Apples are $0.32 a pound.");
    break;
  default:
    console.log("Sorry, we are out of " + expr + ".");
}</pre>

<h3 id="在export语句中使用default"><code>export</code>语句中使用<code>default</code></h3>

<p>如果要导出单个值或需要模块的回调值,则可以使用默认导出: </p>

<pre class="brush: js">// module "my-module.js"
let cube = function cube(x) {
  return x * x * x;
}
export default cube;
</pre>

<p>然后,在另一个脚本中,默认导出将直接被导入:</p>

<pre class="brush: js">// module "my-module.js"
import myFunction from 'my-module';
console.log(myFunction(3)); // 27
</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('ES6', '#sec-switch-statement', 'switch statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-exports', 'Exports')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-switch-statement', 'switch statement')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-exports', 'Exports')}}</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.default")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Statements/export", "export")}}</li>
 <li>{{jsxref("Statements/switch", "switch")}}</li>
</ul>