종우의 컴퓨터 공간
[ES6] Sub Classes 본문
자바 스크립트 (JavaScript)/Object Oriented JavaScript - ES5&ES2015
[ES6] Sub Classes
종우공간 2021. 8. 8. 06:08- ES6 신텍스에서 상속하는 방법이다.
- 자바 언어처럼 extends 키워드를 통해서 상속한다.
예제
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
greeting() {
return `Hello there ${this.firstName} ${this.lastName}`;
}
}
class Customer extends Person {
constructor(firstName, lastName, phone, membership) {
super(firstName, lastName);
this.phone = phone;
this.membership = membership;
}
static getMembershipCost() {
return 500;
}
}
const john = new Customer('John', 'Doe', '801-555-5555', 'Standard');
console.log(john);
console.log(john.greeting());
console.log(Customer.getMembershipCost());
- super() 메소드를 통해서 상속하는 부모의 필드에 값을 넣을 수 있다.
- 이전에 ES5 신텍스에서 프로토타입 객체에 링크를 연결하고 또 자기자신의 컨스트럭터를 추가했던게 자동으로 된다.
'자바 스크립트 (JavaScript) > Object Oriented JavaScript - ES5&ES2015' 카테고리의 다른 글
[ES6] Classes (0) | 2021.08.08 |
---|---|
Using Object.create() (0) | 2021.08.08 |
Prototypes Inheritance (0) | 2021.08.07 |
★Protytypes Explained(프로토타입 기본 개념) (0) | 2021.08.07 |
Built in Constructors (0) | 2021.08.07 |