ES6 class Inheritance 본문

2021 프론트 엔드 로드맵 따라가기/JS

ES6 class Inheritance

알 수 없는 사용자 2021. 6. 5. 19:26

class 키워드를 이용했을 때 상속 구현 방법을 알아보자.

extends 라는 키워드를 이용해 상속받을 클래스를 기재하고, 해당 클래스를 상속받은 클래스의 생성자에 super()를 이용해 부모 클래스의 생성자를 호출할 수 있다.

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);  // Calls parent class constructor
    this.phone = phone;
    this.membership = membership;
  }

  static getMembershipCost() {
    return 500;
  }
}

const jian = new Customer("jian", "dev", "555-555-8555", "Standard");

console.log(jian.greeting());

console.log(Customer.getMembershipCost());

 

 

 

'2021 프론트 엔드 로드맵 따라가기 > JS' 카테고리의 다른 글

비동기(Asynchronous) 프로그래밍이란  (0) 2021.06.06
ES5에서 static 메서드를 선언하기  (0) 2021.06.06
ES6 class  (0) 2021.06.05
[중요] Prototypal inheritance  (0) 2021.06.05
Prototype  (0) 2021.06.05
Comments