constructor(생성자)를 사용하면 다른 모든 메서드 호출보다 앞선 시점인, 인스턴스 객체를 초기화할 때 수행할 초기화 코드를 정의할 수 있다.
class Person {
constructor(name) {
this.name = name;
}
introduce() {
console.log(`Hello, my name is ${this.name}`);
}
}
const otto = new Person('Otto');
otto.introduce(); // Hello, my name is Otto
클래스에 생성자를 정의하지 않으면 기본 생성자를 사용한다. 아무것도 상속하지 않는 기본 클래스일 때의 기본 생성자는 빈 메서드이다.
constructor() {}