(Grub, Bee, ForagerBee, HoneyMakerBee) .js 파일이 있다.
위의 순서대로 상속받는 차례고 각각 js는 조건과 상태가 다르다.
-Grub의 조건
✓ age
속성은 0
이어야 합니다
✓ color
속성은 pink
이어야 합니다
✓ food
속성은 jelly
이어야 합니다
✓ eat
메소드가 존재해야 합니다
✓ eat
메소드를 통해 Grub
이 젤리를 먹습니다
class Grub {
constructor(age, color ,food ,eat) {
this.age = 0
this.color = 'pink'
this.food = 'jelly'
}
eat() {
return 'Mmmmmmmmm jelly'
}
}
module.exports = Grub;
위의 조건대로 코드를 짠 모습!
-bee의 조건
✓ age
속성은 5
이어야 합니다
✓ color
속성은 yellow
이어야 합니다
✓ food
속성은 Grub으로부터 상속받습니다
✓ eat
메소드는 Grub으로부터 상속받습니다
✓ job
속성은 Keep on growing
이어야 합니다
const Grub = require('./Grub');
class Bee extends Grub{
constructor(food, eat){
super(food,eat)
this.age = 5;
this.color = 'yellow'
this.job = 'Keep on growing'
}
}
module.exports = Bee;
bee는 grub의 상속을 받아 bee에 없는 속성을 grub으로부터 받아오고 있다.
extends 와 super을 이용해 grub의 속성을 받아왔다.
받아온 속성은 food, 메소드 eat이다.
-ForagerBee의 조건
✓ age
속성은 10
이어야 합니다
✓ job
속성은 find pollen
이어야 합니다
✓ color
속성은 Bee
로부터 상속받습니다
✓ food
속성은 Grub
으로부터 상속받습니다
✓ eat
메소드는 Grub
으로부터 상속받습니다
✓ canFly
속성은 true
이어야 합니다
✓ treasureChest
속성은 빈 배열 []
이어야 합니다
✓ forage
메소드를 통해 treasureChest
속성에 보물을 추가할 수 있어야 합니다
const Bee = require('./Bee');
const Grub = require('./Grub');
class ForagerBee extends Bee{
constructor(){
super()
this.age = 10
this.job = 'find pollen'
this.canFly = true;
this.treasureChest = []
} forage(treasure) {
return this.treasureChest.push(treasure)
}
}
module.exports = ForagerBee;
잘 살펴보면, ForagerBee에서 없는 속성인 color, food, eat을 상속받고 있는데,
Bee에서 이미 Grub에서 food, eat을 받고 있으니, color만 부모 Grub과 Bee에서 선택하면 된다.
따라서 extends Bee
로 Bee의 color을 상속해준 모습
-HoneyMakerBee의 조건
✓ age
속성은 10
이어야 합니다
✓ job
속성은 make honey
이어야 합니다
✓ color
속성은 Bee
로부터 상속받습니다
✓ food
속성은 Grub
으로부터 상속받습니다
✓ eat
메소드는 Grub
으로부터 상속받습니다
✓ honeyPot
속성은 0
이어야 합니다
✓ makeHoney
메소드는 honeyPot
에 1씩 추가합니다
✓ giveHoney
메소드는 honeyPot
에 1씩 뺍니다
onst Bee = require('./Bee');
class HoneyMakerBee extends Bee{
constructor(color, food, eat){
super(color, food, eat)
this.age = 10
this.job = 'make honey'
this.canFly = true;
this.honeyPot = 0
}
makeHoney() {
return this.honeyPot++
}
giveHoney() {
return this.honeyPot--
}
}
module.exports = HoneyMakerBee;
HoneyMakerBee도 ForagerBee와 마찬가지로 Bee와 Grub에서 상속받고 있는 모습이다.
🎃위의 코드에서 내가 놓친점
나는 생성자 constructor에 color, food, eat 등 인자를 직접 넣어주고 있는데,
넣어주지 않아도 결과값은 같다.
그렇다면! 인자를 직접 넣을때는 어떤 경우일까?
예제로 확인해보면,
class Human {
constructor(name, age){
this.name = name;
this.age = age;
}
sleep(){ return `${this.name}이(가) 잠을 잡니다.`}
}
class Student extends Human{
constructor(name, age, grade){
super(name, age);
this.grade = grade;
}
study(num){
this.grade = this.grade + num;
return `${this.name}의 성적이 ${num}만큼 올라 ${this.grade}이 되었습니다.`
}
}
class ForeignStudent extends Student{
constructor(name, age, grade, country){
super(name, age, grade);
this.country = country;
}
speak(){
return `${this.country} 언어로 수업을 진행합니다.`
}
}
let americanStudent = new ForeignStudent('jake', 23, 80, '미국');
위에서 americanStudent라는 인스턴스로 ForeignStudent의 인자값을 전달받는다.
따라서 인스턴스를 만들때 class에 인자를 넣어준다.
🧨return 의 중요성
const Bee = require('./Bee');
const Grub = require('./Grub');
class ForagerBee extends Bee{
constructor(){
super()
this.age = 10
this.job = 'find pollen'
this.canFly = true;
this.treasureChest = []
} forage(treasure) {
return this.treasureChest.push(treasure)
}
}
module.exports = ForagerBee;
위 예제에서 내가 놓친것!
forage(treasure) {
return this.treasureChest.push(treasure)
여기에서 return을 넣으면 아주 위험하다!
이유는? push, pop, unshift, shift 등의 내장함수 값의 리턴값이
const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
console.log(plants.pop());
// Expected output: "tomato"
위와 같이 배열이 아니라 값 그 자체를 return 할 수도 있기 때문이다!