JavaScript Class super 키워드

정의와 사용법

super 키워드를 부모 클래스로 참조합니다。

부모 클래스의 생성자를 호출하고 부모 클래스의 속성과 메서드에 접근하는 데 사용됩니다。

팁:"상속" 개념(부모 클래스와 자식 클래스)을 더 잘 이해하려면 우리의 JavaScript 클래스 교본

인스턴스

이름이 "Model"인 클래스를 생성하여 사용하여 extends 키워드 "Car" 클래스의 메서드를 상속합니다.

생성자 메서드에서 호출하여 super() 메서드를 호출하면 부모 클래스의 생성자를 호출할 수 있으며 부모 클래스의 속성과 메서드에 접근할 수 있습니다:

class Car {
  constructor(brand) {
    this.carname = brand;
  }
  present() {
    return 'I have a ' + this.carname;
  }
}
class Model extends Car {
  constructor(brand, mod) {
    super(brand);
    this.model = mod;
  }
  show() {
    return this.present() + ', it is a ' + this.model;
  }
}
mycar = new Model("Ford", "Mustang");
document.getElementById("demo").innerHTML = mycar.show();

직접 시도해보세요

문법

super(arguments);  // 부모 생성자 호출(생성자 내에서만)
super.parentMethod(arguments);  // 부모 메서드 호출

기술 세부 사항

JavaScript 버전: ECMAScript 2015 (ES6)

브라우저 지원

키워드 크롬 IE 파이어폭스 사파리 오페라
super 42.0 13.0 45.0 9.0 36.0

관련 페이지

JavaScript 교본:JavaScript 클래스

JavaScript 교본:JavaScript ES6 (EcmaScript 2015)

JavaScript 참조 매뉴얼:extends 키워드

JavaScript 참조 매뉴얼:constructor() 메서드