객체를 매번 만들어서 사용해도 좋지만, 큰 큐모의 객체이거나 비슷한 모양의 객체를 계속 만들어야 한다면, class라는 설계도를 통해 만들 수 있다.
// 클래스 선언문
class Person { // 첫글자는 대문자로 시작
// constructor(생성자)
constructor(name) {
this._name = name;
}
}
class Person {
constructor(name) {
this._name = name;
}
// Person객체에 sayHi라는 메서드 추가!
sayHi() {
console.log(`Hi! ${this._name}`);
}
}
// 인스턴스 생성
const me = new Person('Lee');
me.sayHi(); // Hi! Lee
console.log(me instanceof Person); // true