클래스(Class)는 “특별한 함수”이다. 함수를 함수 표현식과 함수 선언문으로 구분할 수 있듯이 클래스도 클래스 선언문과 클래스 표현식으로 표현할 수 있습니다.
클래스명과 class 키워드를 사용해 선언합니다.
class Person{
constructor(name, age){
this.name = name;
this.age = age;
}
}
표현식은 익명일수도 있고 기명일수도 있습니다. 이름을 가진(기명) 클래스일 경우 그 클래스 body의 local scope 에만 유효합니다.
const person = class {
constructor(name){
this.name = name;
}
}
console.log(person.name) // person <- const뒤에 식별자 person을 말하는거임
const person = class Person2 {
constructor(name){
this.name = name;
}
}
console.log(person.name); // Person2
클래스는 호이스팅이 일어나지만 const, let와 같이 초기화가 일어나지 않기때문에 접근이 불가능합니다. 런타임에서 클래스를 정의한 후 사용이 가능합니다.
class 로 생성된 객체를 생성하고 초기화 하기위한 특수한 메서드 입니다. 클래스 안에 한 개의 생성자만 존재할 수 있습니다.
클래스의 본문(body)는 strict mode에서 실행됩니다. strict모드로 설정안해도 클래스는 strict모드입니다.
정적 (static) 속성과 프로토타입 속성은 클래스 선언부 바깥쪽에서 정의해야합니다.
정적 속성과 정적 메서드는 인스턴스를 통해 호출할 수 없습니다. 클래스 명으로 호출 가능합니다
// 인스턴스 속성
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// 잠시 아래 호출 예시를 들기 위해 추가함
static displayName = 'Point'
}
// 정적 속성과 프로토타입 속성을 바깥쪽에서 정의함.
Rectangle.staticWidth = 20;
Rectangle.prototype.prototypeWidth = 25;
//정적 속성 호출
Rectangle.displayName; // 알맞은 정적 호출
const r = new Rectangle(30, 60);
r. displayName // error 발생
정적 속성,프로토타입 속성을 제외한
다음 시간에 추가적으로 알아봐야겠습니다!