非常教程

JavaScript参考手册

声明 | Statements

default

default 关键字可以在 JavaScript 的两种情况下使用:在 switch,或 export中。

语法

switch语句中使用:

switch (expression) {
  case value1:
    //Statements executed when the result of expression matches value1
    [break;]
  default:
    //Statements executed when none of the values match the value of the expression
    [break;]
}

export中使用:

export default nameN 

描述

更多细节,参见

  • switch 语句和

  • export 语句页面。

示例

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)The definition of 'switch statement' in that specification.

Standard

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Exports' in that specification.

Standard

ECMAScript Latest Draft (ECMA-262)The definition of 'switch statement' in that specification.

Draft

ECMAScript Latest Draft (ECMA-262)The definition of 'Exports' in that specification.

Draft

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Switch default

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

Export default

No support

?

No support

No support

No support

No support

Feature

Android

Chrome for Android

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Switch default

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

Export default

No support

No support

?

No support

No support

No support

No support

JavaScript

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络