class Milk {
// 생성자
constructor(taste, emoji) {
this.taste = taste;
this.emoji = emoji;
}
// 클래스 레벨의 메서드
static random() {
return new Milk('banana', '🍌');
}
display(){
console.log(`${this.taste}: ${this.emoji}`);
}
}
// 클래스 레벨 이름으로 접근
const bana = Milk.random();
console.log(bana);
// apple은 Fruit의 인스턴스이다.
// orange는 Fruit의 인스턴스이다.
const choco = new Milk('chocolate', '🍫');
const berry = new Milk('strawberry', '🍓');
choco.display();
berry.display();
👉 클래스 레벨의 메서드에서는 주어진 데이터가 채워져 있지 않을 상태이기 때문에 this를 참조 할 수 없다.
👉 클래스 레벨의 함수는 클래스 레벨의 이름으로 접근이 가능하고
인스턴스 레벨의 프로퍼티와 함수는 만들어진 인스턴스를 통해서 접근이 가능하다.