코딩물고기의 IT월드
[자바스크립트] constructor
코딩하는물고기
2020. 8. 24. 22:36
728x90
class Animal {
constructor(type, name, sound) {
this.type = type;
this.name = name;
this.sound = sound;
}
say() {
console.log(this.sound);
}
}
const dog = new Animal("개", "멍멍이", "멍멍");
const cat = new Animal("고양이", "야옹이", "야옹");
dog.say();
cat.say();
------------------------------------------------------------------------
extended
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) {}
}
const dog = new Animal("개", "멍멍이", "멍멍");
const cat = new Animal("고양이", "야옹이", "야옹");
const cat2 = new Animal("야옹이", "야옹야옹이", "야야옹");
dog.say();
cat.say();
cat2.say();
---------------------------------------------------------------
super 추가
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 cat2 = new Cat("야옹야옹이", "야야옹");
dog.say();
cat.say();
cat2.say();
-------------------------------------------------------------
constructor
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("BBQ");
pizza.print();
chicken.print();
728x90