これは Harmony(ECMAScript 6) 提案の一部であり、実験段階の技術です。
この技術の仕様は安定していません。ブラウザ互換性の一覧表を確認してください。またこれらの構文や動作は、仕様変更などにより、新しいバージョンのブラウザでは変更される可能性があるという点に注意してください。
static キーワードは、クラスに静的メソッドを定義します。
構文
static methodName() { ... }
説明
インスタンス化せずに呼び出されたクラスの静的メソッドは、クラスがインスタンス化された場合でも呼び出し可能です。静的メソッドは、アプリケーションのためのユーティリティ関数を作成するためによく使われます。
例
次の例は、いくつかのことをデモしています。この例は、静的メソッドがクラスにどのように実装されるか、そして、静的なメンバを持つクラスがどのようにサブクラスになるかを示します。最後に、静的メソッドがどのように呼び出せるか、または呼び出せないかを示します。
class Tripple {
static tripple(n) {
n = n | 1;
return n * 3;
}
}
class BiggerTripple extends Tripple {
static tripple(n) {
return super.tripple(n) * super.tripple(n);
}
}
console.log(Tripple.tripple());
console.log(Tripple.tripple(6));
console.log(BiggerTripple.tripple(3));
var tp = new Tripple();
console.log(tp.tripple()); //Logs 'tp.tripple is not a function'.
仕様
| 仕様 | 状況 | コメント |
|---|---|---|
| ECMAScript 6 (ECMA-262) The definition of 'Class definitions' in that specification. |
勧告候補 | Initial definition. |
ブラウザ実装状況
| 機能 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| 基本サポート | 42.0 | Available in the Nightly channel only (since February 2015) | ? | ? | ? |
| 機能 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| 基本サポート | ? | 42.0 | Available in the Nightly channel only (since February 2015) | ? | ? | ? |

