과제 문제해설

지원 ·2023년 5월 12일

1번 문제

레퍼런스 코드

class Grub {
  constructor(age = 0, color = 'pink', food = 'jelly'){
    this.age = age;
    this.color = color;
    this.food = food;
  }

  eat() {
    return `Mmmmmmmmm jelly`
  }
}

module.exports = Grub;

Grub 클래스에서 constructor를 사용해 Grub의 property를 매개변수로 입력한다.
위에있는 코드에서는 각 변수에 값이 할당되어 있지만, 원래 할당 값은 생략이 가능하고, 변수만 선언해도 된다. 하지만 아래 코드에서는 값이 할당되어 있기 때문에, this.age = age에 age 값은 0, color 값은 'pink', food 값은 'jelly'로 고정된 것을 확인할 수 있다.

그래서 만약 인스턴스 james = new Grub()를 만들면 값이 그대로 참조되는 것을 볼 있다.

james.age  // 0 
james.color // 'pink' 

그리고 eat() {'return Mmmmmm jelly'}
이것은 클래스 안에서 사용되는 메서드이며 function keyword만 제외된 함수라고 보면 된다.

나의 코드

class Grub {
  // TODO..
  constructor() {
    this.age = 0;
    this.color = 'pink';
    this.food = 'jelly';
  }

  eat() {
    return `Mmmmmmmmm jelly`;
  }
}

module.exports = Grub;

2번 문제

레퍼런스 코드

const Grub = require('./1-Grub');

class Bee extends Grub {
  constructor(age = 5, color = 'yellow', food, job = 'Keep on growing') {
    super(age, color, food);
    this.job = job;
  }
}

module.exports = Bee;

extends를 사용하면 자식 클래스를 생성한다는 것이다.
Bee는 Grub의 속성을 그대로 상속 받지만, 이를 사용해도 되고, 변경해도 돼고, 아니면 생략해도 된다.

여기서 constructor의 역할은 동일하다. 다만, 새로운 값은 할당할 수 있다. 새로 할당된 값은 부모 클래스 Grub에게 영향을 미치지 않기 때문에 무엇인든 할당 할 수 있다.

그리고 여기서 super라는 것은 부모 클래스 속성을 불러오느 것이다. super(age, color, food)는 특정 property를 불러오고, super()만 사용하면 부모의 모든 property를 불러온다.

그리고 Bee에서는 새로운 property 할당되는 것을 확인 할 수 있다.

  this.job = job; //"Keep on growing" 

나의 코드

const Grub = require('./1-Grub');

class Bee extends Grub {
  // TODO..
  constructor() {
    super();
    this.color = 'yellow';
    this.age = 5;
    this.job = 'Keep on growing';
  }
}

module.exports = Bee;

3번 문제

레퍼런스 코드

const Bee = require('./2-Bee');

class ForagerBee extends Bee {
  constructor(age = 10, color, food, job = 'find pollen', canFly = true) {
    super(age, color, food, job);
    this.canFly = canFly;
    this.treasureChest = [];
  }

  forage(treasure) {
    this.treasureChest.push(treasure);
  }
}

module.exports = ForagerBee;

여기서 extend와 super의 역할은 2번문제와 동일하다.
다만, ForagerBee 클래스는 Bee의 자식 클래스기 된다.
Bee는 Grub에게 자식이고, ForagerBee는 Bee의 자식이기 때문에 Grub의 propert 포함한 Bee를 그대로 상속되는 것이다.

ForagerBee에서는 새로운 property this.canFly = canFly가 추가되었고, 빈배열 변수도 선언된 것을 확인할 수 있다.

그리고 forage 메서드에 (treasure)매개변수를 받아, constructor에 선언된 treasureChest에 입력 받은 매개변수를 push 되는 것이다. 이를 이용해 새로운 배열이 생성된다.

나의 코드

const Bee = require('./2-Bee');

class ForagerBee extends Bee {
  // TODO..

  constructor() {
    super();
    this.age = 10;
    this.job = 'find pollen';
    this.canFly = true;
    this.treasureChest = [];
  }

  forage(e) {
    return this.treasureChest.push(e);
  }
}

module.exports = ForagerBee;

4번 문제

레퍼런스 코드

const Bee = require('./2-Bee');

class HoneyMakerBee extends Bee{
  constructor(age = 10, color, food, job = 'make honey') {
    super(age, color, food, job);
    this.honeyPot = 0;
  }

  makeHoney() {
    this.honeyPot += 1;
  }

  giveHoney() {
    this.honeyPot -= 1;
  }
}

module.exports = HoneyMakerBee;

1번부터 3번까지 설명한 내용은 생략하고, 4번 문저에서는 메서드만 살펴보면 된다.

우선, constructor 안에 (this.honeyPot = 0값이 할단 된 것을 볼 수 있다.

makeHoney() 메서드에서 increment 가 발생하는 것은 확인할 수 있다
this.honeyPot = this.honeyPot + 1;
또는 this.honeyPot++ 사용도 가능하다

그리고 반대로 decrement 메서드는 giveHoney()
this.honeyPot = this.honeyPot - 1;
또는 this.honeyPot-- 사용도 가능하다

나의 코드

const Bee = require('./2-Bee');

class HoneyMakerBee extends Bee {
  // TODO..
  constructor() {
    super();
    this.age = 10;
    this.job = 'make honey';
    this.honeyPot = 0;
  }

  makeHoney() {
    this.honeyPot++;
  }

  giveHoney() {
    this.honeyPot--;
  }
}

module.exports = HoneyMakerBee;

0개의 댓글