아래 링크 참조
class Food {
// 필드
strFood; // public 프로퍼티
#country; // private 프로퍼티
// 생성자
constructor(country) {
this.intFood = 55; // public 프로퍼티, 생성자에서 정의하면 필드에 없어도 프로퍼티 생성
this.#country = country;
this.strFood = 'Food Food';
}
// getter
get country() {
return this.#country;
}
// setter
set country( country ) {
this.#country = country;
}
// static 메소드
static print() {
console.log('Food : static print');
}
}
const food = new Food('한국');
인스턴스화(Instantiate)
new 키워드를 사용하여 인스턴스를 생성const methodInstance = new Method();
console.log(methodInstance.add(2, 3));
class Method {
add(a, b) {
return a + b;
}
}
class Whale {
// 인스턴스 필드
gender;
// 클래스 정적 필드
static name = 'Whale';
swimming() {
// 지역 변수
const swimm = "헤엄칩니다.";
console.log(swimm);
}
}
const myWhale = new Whale();
myWhale.gender = 'female';
console.log(myWhale.gender); // "female"
console.log(Whale.name); // "Whale"