super

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2016.

super キーワードは、オブジェクトの親の関数を呼び出すために使用できます。

super.prop および super[expr] 式は、classオブジェクトリテラルの両方におけるあらゆるメソッド定義で有効です。

試してみましょう

class Foo {
  constructor(name) {
    this.name = name;
  }

  getNameSeparator() {
    return "-";
  }
}

class FooBar extends Foo {
  constructor(name, index) {
    super(name);
    this.index = index;
  }

  // Does not get called
  getNameSeparator() {
    return "/";
  }

  getFullName() {
    return this.name + super.getNameSeparator() + this.index;
  }
}

const firstFooBar = new FooBar("foo", 1);

console.log(firstFooBar.name);
// Expected output: "foo"

console.log(firstFooBar.getFullName());
// Expected output: "foo-1"

構文

js
super([arguments]); // 親コンストラクターを呼び出します。
super.functionOnParent([arguments]);

解説

コンストラクターで使用する場合、super キーワードを単独で置き、this キーワードが使われる前に使用する必要があります。super キーワードは、親オブジェクトの関数を呼び出すためにも使用できます。

クラス内での super の使用

このコードスニペットは、クラスの例 (ライブデモ) からとっています。super() を利用することで、RectangleSquare のコンストラクターに共通する処理を重複して記述しないようにしています。

js
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._area = 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";
  }
}

静的メソッドでの super の呼び出し

static メソッドでも super を呼び出すことができます。

js
class Rectangle {
  static logNbSides() {
    return "I have 4 sides";
  }
}

class Square extends Rectangle {
  static logDescription() {
    return super.logNbSides() + " which are all equal";
  }
}
Square.logDescription(); // 'I have 4 sides which are all equal'

super プロパティを削除するとエラーが発生する

親クラスのプロパティを削除するために、delete 演算子super.propsuper[expr] を使うことはできません。ReferenceError がスローされます。

js
class Base {
  foo() {}
}
class Derived extends Base {
  delete() {
    delete super.foo; // this is bad
  }
}

new Derived().delete(); // ReferenceError: invalid delete involving 'super'.

super.prop は書き込み不可能なプロパティを上書きできない

Object.defineProperty などで書き込み不可プロパティを定義した場合、super はプロパティの値を上書きできません。

js
class X {
  constructor() {
    Object.defineProperty(this, "prop", {
      configurable: true,
      writable: false,
      value: 1,
    });
  }
}

class Y extends X {
  constructor() {
    super();
  }
  foo() {
    super.prop = 2; // 値を上書きできない
  }
}

var y = new Y();
y.foo(); // TypeError: "prop" は読み取り専用
console.log(y.prop); // 1

オブジェクトリテラル内での super.prop の使用

super はオブジェクト初期化子 / リテラル記法内でも使用できます。この例では、 2 つのオブジェクトがメソッドを定義しています。 2 つ目のオブジェクトの中で、super が最初のオブジェクトのメソッドを呼び出しています。これは Object.setPrototypeOf() の助けで動作し、これは obj2 のプロトタイプを obj1 に設定するので、supermethod1obj1 上で見つけることができます。

js
var obj1 = {
  method1() {
    console.log("method 1");
  },
};

var obj2 = {
  method2() {
    super.method1();
  },
};

Object.setPrototypeOf(obj2, obj1);
obj2.method2(); // logs "method 1"

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-super-keyword

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
super

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報