📌 class레벨에서는 접근❌
class Fruits {
constructor(name, color){
this.name = name;
this.color = color;
}
display = () => {
console.log(`${this.name}: ${this.color}`);
};
}
const apple = new Fruit("apple", "red");
const orange = new Fruit("orange", "orange");
console.log(Fruit.name); //->❌
Fruit.display(); //->❌
class Fruits {
static MAX_FRUITS = 100;
constructor(name, color){
this.name = name;
this.color = color;
}
static makeRandomFruits() {
return new Fruit("melon", "green");
//새로운 object를 만들어서 반환하는 함수
}
display = () => {
console.log(`${this.name}: ${this.color}`);
};
}
console.log(Fruits.MAX_FRUITS); //100
const melon = Fruits.makeRandomFruits();
console.log(melon);
//Fruit { display: [Function: display], name: 'melon', color: 'green' }
🚩 만들어진 object에서 호출하는 것이 아니라, class이름으로 바로 사용할 수 있다.
✏ static MAX_FRUITS
✏ static makeRandomFruits()
✏ display = () =>