객체 지향 프로그래밍(Object-Oriented Programming, OOP)

클래스(Class)란?

JavaScript에서의 Class란?

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('한국');

인스턴스(Instance) 생성 및 사용

프로퍼티(Property)

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"

메소드(Method)