The Wayback Machine - https://web.archive.org/web/20160704003108/https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/default

default

この記事は編集レビューを必要としています。ぜひご協力ください

defaultキーワードは、JavaScriptにおいて2つのシチュエーションで使われます。: 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が"Bananas"か"Apples"と評価された場合、プログラムは、case "Bananas" か case "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を使う

単一の値やモジュールのためのフォールバック値をエクスポートしたい場合、default exportが使用できます:

// module "my-module.js"
let cube = function cube(x) {
  return x * x * x;
}
export default cube;

続いて、ほかのスクリプトで、 簡単にdefault exportをインポートできます:

// module "my-module.js"
import myFunction from 'my-module';
console.log(myFunction(3)); // 9

仕様

仕様 状態 コメント
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 2016 Draft (7th Edition, ECMA-262)
The definition of 'switch statement' in that specification.
ドラフト  
ECMAScript 2016 Draft (7th Edition, ECMA-262)
The definition of 'Exports' in that specification.
ドラフト  

ブラウザ実装状況

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Switch default (有) (有) (有) (有) (有)
Export default 未サポート 未サポート 未サポート 未サポート 未サポート
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Switch default (有) (有) (有) (有) (有) (有)
Export default 未サポート 未サポート 未サポート 未サポート 未サポート 未サポート

関連項目

ドキュメントのタグと貢献者

タグ: 
 このページの貢献者: YuichiNukiyama
 最終更新者: YuichiNukiyama,
HTTPS · web.archive.org
← Home