これは Harmony(ECMAScript 6) 提案の一部であり、実験段階の技術です。
この技術の仕様は安定していません。ブラウザ互換性の一覧表を確認してください。またこれらの構文や動作は、仕様変更などにより、新しいバージョンのブラウザでは変更される可能性があるという点に注意してください。
superキーワードは、オブジェクトの親の関数を呼び出すために使用できます。
super.prop および super[expr] 式は、class と オブジェクトリテラル の両方におけるどのようなメソッド定義でも有効です。
構文
super([arguments]); // 親コンストラクタを呼び出す。 super.functionOnParent([arguments]);
説明
コンストラクタ内で使用する場合、super キーワードを単独で置き、this キーワードが使われる前に使用しなくてはなりません。このキーワードは、親オブジェクトの関数を呼び出すためにも使用できます。
例
このコードスニペットは、classes sample (実際のデモ) からとっています。
class Square extends Polygon {
constructor(length) {
// Here, it calls the parent class' constructor with lengths
// provided for the Polygon's width and height
super(length, length);
// Note: In derived classes, super() must be called before you
// can use 'this'. Leaving this out will cause a reference error.
this.name = 'Square';
}
get area() {
return this.height * this.width;
}
set area(value) {
this.area = value;
}
}
仕様
| 仕様 | 状況 | コメント |
|---|---|---|
| ECMAScript 6 (ECMA-262) The definition of 'super' in that specification. |
勧告候補 | Initial definition. |
ブラウザ実装状況
| 機能 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| 基本サポート | 42.0 | 未サポート バグ 1066239 |
? | ? | ? |
| 機能 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| 基本サポート | ? | 42.0 | 未サポート バグ 1066239 |
? | ? | ? |