aotoyae

[JS] class 상속 extends 본문

JavaScript

[JS] class 상속 extends

aotoyae 2023. 9. 16. 15:11

 

 

class Person {
  constructor(name, first, second, third) {
    this.name = name;
    this.first = first;
    this.second = second;
    this.third = third;
  }
  sum() {
    return "prototype : " + (this.first + this.second + this.third);
  }
}
class PersonPlus extends Person {
  avg() {
    return (this.first + this.second + this.third) / 3;
  }
}

var kim = new PersonPlus("kim", 10, 20, 30);
console.log("kim.sum()", kim.sum()); //60
console.log("kim.avg()", kim.avg()); //20

✳️ Person 의 코드를 그대로 상속받으니 추가로 넣고싶은 기능만 적으면 된다.