객체 지향 프로그래밍(OOP)에서 특정 객체를 생성하기 위해 변수와 메소드를 정의하는 일종의 template이다.
자바스크립트의 class는 Prototype을 사용하며 작동한다는 특이점이 존재.
부모 Class에서 객체가 상속하며 받아온다라고 생각하자.
class Person {
constructor(name, email, birthday) {
this.name = name;
this.email = email;
this.birthday = new Date(birthday);
}
// 메소드 생성하기.
introduce() {
return `Hello my name is ${this.name}`;
}
static multiplenNumber(x,y){
return x * y;
}
}
const john = new Person ('John', '[email protected]', '10-3-98');
console.log(john)
constructor는 인스턴스의 생성과 동시에 클래스 필드의 생성을 초기화를 실행한다??
이게 무슨 말인지 GPT에 물어봤다.
Class를 통해 생성했을 경우, 메소드는 Prototype안에 주소로 입력되어 있다.
(생성자 함수와는 다름, → 메모리 적 효율성 상승)