자바스크립트의 상속

June Lee·2021년 3월 10일

JavaScript

목록 보기
6/11

class를 이용해서 객체를 생성하는 경우, 자바스크립트에서도 자바에서와 마찬가지로 상속도 구현해줄 수 있다. 그 예시는 다음과 같다.

class Rectangle {
  constructor(w, h){
    this.width = w;
    this.height = h;
  }
  getArea(){
    return this.width * this.height;
  }
}

class Square extends Rectangle{
  constructor(length){
    super(length, length);
  }
}
let square = new Square(10);
console.log(square.getArea()); 
profile
📝 dev wiki

0개의 댓글