JavaScript Class extends 키워드
- 이전 페이지 constructor()
- 다음 페이지 static
- 하나层次 뒤로 JavaScript Class 참조 매뉴얼
정의와 사용법
extends
키워드는 다른 클래스(부모)의 자식 클래스를 생성하는 데 사용됩니다.
자식 클래스는 다른 클래스의 모든 메서드를 상속합니다.
상속은 코드의 재사용성에 매우 유용합니다: 새 클래스를 생성할 때, 기존 클래스의 속성과 메서드를 재사용할 수 있습니다.
주의사항:위의 예제에서 볼 수 있듯이,super()
메서드는 부모 클래스를 참조합니다. 생성자 메서드에서 호출하여 super()
메서드를 호출할 수 있으며, 부모 클래스의 속성과 메서드에 접근할 수 있습니다.
인스턴스
이름을 "Model"로 하는 클래스를 생성하여 "Car" 클래스의 메서드를 상속받습니다:
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();
문법
class childClass extends parentClass
기술 세부 사항
JavaScript 버전: | ECMAScript 2015 (ES6) |
---|
브라우저 지원
키워드 | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
extends | 49.0 | 13.0 | 45.0 | 9.0 | 36.0 |
관련 페이지
JavaScript 교본:JavaScript 클래스
JavaScript 교본:JavaScript ES6 (EcmaScript 2015)
JavaScript 참조 메뉴얼:super 키워드
JavaScript 참조 메뉴얼:constructor() 메서드
- 이전 페이지 constructor()
- 다음 페이지 static
- 하나层次 뒤로 JavaScript Class 참조 매뉴얼