语法
在switch
语句中使用:
switch (expression) { case value1: //当表达式的值和value1匹配执行这里的语句 [break;] default: //当表达式的值没有匹配,执行这里的语句 [break;] }
在export
中使用:
export default nameN
描述
示例
在switch语句中使用default
在以下示例中,如果expr
为“Oranges”或“Apples”,程序将匹配“Oranges”或“Apples”的值并执行相应的声明。在任何其它情况下,default
关键字将执行关联的语句。
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 + ".");
}
在export语句中使用default
如果要导出单个值或需要模块的回调值,则可以使用默认导出:
// module "my-module.js"
let cube = function cube(x) {
return x * x * x;
}
export default cube;
然后,在另一个脚本中,默认导出将直接被导入:
// module "my-module.js"
import myFunction from 'my-module';
console.log(myFunction(3)); // 27
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) switch statement |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Exports |
Standard | |
ECMAScript (ECMA-262) switch statement |
Living Standard | |
ECMAScript (ECMA-262) Exports |
Living Standard |
浏览器兼容
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
No compatibility data found. Please contribute data for "javascript.statements.default" (depth: 1) to the MDN compatibility data repository.