→
オブジェクトの親の関数を呼び出すために使用できます。
親コンストラクターを呼び出す:
arguments | 引数リスト(カンマで区切る) |
---|---|
functionOnParent | ¶ |
super.prop および super[expr] 式は、class と オブジェクトリテラル の両方におけるあらゆるメソッド定義で有効です。
コンストラクターで使用する場合、super キーワードを単独で置き、this キーワードが使われる前に使用する必要があります。super キーワードは、親オブジェクトの関数を呼び出すためにも使用できます。
class Rectangle { constructor(height, width) { this.name = 'Rectangle'; this.height = height; this.width = width; } sayName() { console.log('Hi, I am a ', this.name + '.'); } get area() { return this.height * this.width; } set area(value) { this.height = this.width = Math.sqrt(value); } } class Square extends Rectangle { constructor(length) { this.height; // ReferenceError になります。super を先に呼び出さなければなりません! // length の値で親クラスの constructor を呼びます。 // Rectangle の width と height になります。 super(length, length); // Note: 'this' を使う前に super() をコールしなければなりません。 // でないと reference error になります。 this.name = 'Square'; } }
¶