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(", "));
}
}
푸드라는 클래스를 만들었다. 파라미터는 name을 받아오고 this.brands라는 배열을 정의하였다. 메소드는 addBrand()이고 brand값을 this.brands 배열에 추가하기 위해 push()함수를 사용했고, 브랜드에 들어간 배열 값들을 문자열로 추가하고 seperator를 설정하여 , 이 후 출력한다.
출력되는 것은
피자 파는 음식점들 :
반올림피자, 도미노피자
김밥 파는 음식점들 :
김밥헤븐, 고봉민김밥