Using Class Keyword

devPomme·2021년 1월 19일
0
post-thumbnail

원형 클래스 만들기

class CatClass {
  constructor(name, colors, age, sound) {
    this.name = name;
    this.colors = colors;
    this.age = age;
    this.sound = sound;
    this.cry(sound);
  }
  cry(sound) {
    let catCry = `${this.sound}!`
    return catCry;
  }
  
}

하위 클래스 만들기

엄마 고양이 만들기

class MommyCat extends CatClass {
  constructor(name, color, age, sound){
    super(name, color, age, sound); // 부모 생성자 호출
    this.baby = [];
  }
   haveBaby(baby1, ...rest){
     this.baby.push(baby1, ...rest);
   }
}

인스턴스 만들기

삼색이 인스턴스를 엄마 고양이 클래스로 만들면

const SamSaek = new MommyCat('삼색이', 'black, white, brown', 3, '삐약')
SamSaek.haveBaby('DoDo', 'Marilyn')
SamSaek.baby // ['DoDo', 'Marilyn']

고양이 삼색이의 딸 도도를 인스턴스로 만들어보겠다.

const DoDo = new CatClass('도도', 'black, white', 2, 'meow')
DoDo.name // '도도'
DoDo.cry // 'meow!'
profile
헌신하고 확장하는 삶

0개의 댓글