반응형
class Animal {
constructor(type, name, sound) {
this.type = type;
this.name = name;
this.sound = sound;
}
say() {
console.log(this.sound);
}
}
//상속
class Dog extends Animal {
constructor(name, sound) {
super("개", name, sound);
}
}
class Cat extends Animal {
constructor(name, sound) {
super("고양이", name, sound);
}
}
const dog = new Dog("멍멍이", "멍멍");
const cat = new Cat("야옹이", "야옹");
const cats = new Cat("고양이들", "야오오오오오옹");
dog.say();
cat.say();
cats.say();
class Food {
constructor(name) {
this.name = name;
this.brands = [];
}
addBrand(brand) {
this.brands.push(brand);
}
print() {
console.log(`${this.name}을/를 파는 음식점들:`);
console.log(this.brands.join(", "));
}
}
const pizza = new Food("피자");
pizza.addBrand("피자헛");
pizza.addBrand("도미노피자");
const chicken = new Food("치킨");
chicken.addBrand("굽네치킨");
chicken.addBrand("동네앞치킨");
pizza.print();
chicken.print();
반응형
'2020 > Vanilla JavaScript' 카테고리의 다른 글
Vanilla JavaScript # 모달 만들기 (0) | 2020.03.29 |
---|---|
Javascript #prototype (0) | 2020.03.28 |
JavaScript #내장함수 reduce (0) | 2020.03.28 |
JavaScript #내장함수 (shift, pop, unshift, push, concat, join) (0) | 2020.03.28 |
JavaScript splice vs slice (0) | 2020.03.28 |