class 클래스 이름 {
// 클래스 내용
}예시
class Rectangle {
// 클래스 내용
}
let 객체이름 = new 클래스이름();예시
class Rectangle {
// 클래스 내용
}
let rec1 = new Rectangle();

class Rectangle {
constructor() {
// 객체 초기화
}
}
class Rectangle {
// 생성자 메서드에 매개변수 선언
constructor(**w, h**) {
// 인스턴스 프로퍼티
this.width = w;
this.height = h;
}
}
// 객체 생성 시, 초기값 전달
let rec1 = new Rectangle(100, 200);
return문은 생략해야 함
new 연산자가 클래스와 함께 호출되면 암묵적으로 객체를 반환해주기 때문
new가 return을 포함하고 있음
class Rectangle {
constructor(w, h) {
this.width = w;
this.height = h;
~~return object;~~
}
}
// 객체 생성 시, 초기값 전달
let rec1 = new Rectangle(100, 200);
명시적으로 객체를 리턴하는 경우
- 객체를 만들었기 때문에 객체를 return
class Rectangle {
constructor(w, h) {
this.width = w;
this.height = h;
**return {};**
}
}
let rec1 = new Rectangle(100, 200);
console.log(rec1); // 결과 : {} 빈 객체 리턴
명시적으로 다른 값을 반환하는 경우
- return값이 반환되지 않고 Rectangle이 반환
- return을 쓰나 안 쓰나 결과값 동일
class Rectangle {
constructor(w, h) {
this.width = w;
this.height = h;
**return “soo”;**
}
}
let rec1 = new Rectangle(100, 200);
console.log(rec1);
// 결과 : Rectangle { width: 100, height: 200 }
객체이름.프로퍼티이름

class Rectangle {
constructor(w, h) {
this.width = w;
this.height = h;
}
// 프로토타입 메서드
area() {
console.log('사각형의 넓이를 구합니다.');
}
}
let rec1 = new rectangle(100, 200);
// 클래스 내부의 area() 메서드 호출
rec1.area();
class Rectangle {
constructor(w, h) {
this.width = w;
this.height = h;
}
area = () => {
console.log("사각형의 넓이를 구합니다.");
}
}

클래스는 프로퍼티(값)와 메서드(함수)로 이루어져 있다
클래스 자체에 선언된 프로퍼티 또는 메서드
객체를 생성하지 않아도 호출 가능
객체가 아닌 클래스 자체가 갖는 값
선언 방법
- static 키워드를 붙여서 생성
class Rectangle {
**static** color = “red”;
constructor(w, h) {
this.width = w;
this.height = h;
}
**static** area = () => {
console.log("사각형의 넓이를 구합니다.");
}
}
객체가 호출하지 않고, 클래스로 호출해야 함
클래스.정적메서드()
Rectangle.area();
| 구분 | 정적 메서드 | 프로토타입 메서드 |
|---|---|---|
| 프로토타입 체인 | 클래스 | 인스턴스(객체) |
| 호출 방식 | 클래스로 호출 | 인스턴스로 호출 |
| 인스턴스 프로퍼티 참조 가능 여부 (this에 접근 가능한가) | 불가능 | 가능 |
class Rectangle {
**#color = "red";**
constructor(w, h) {
this.width = w;
this.height = h;
}
area = () => {
console.log("사각형의 넓이를 구합니다.");
}
}
var rec1 = new Rectangle(100, 200);
console.log(**rec1.color**); // 결과 : undefinedclass Rectangle {
**#color = "red";**
constructor(w, h) {
this.width = w;
this.height = h;
}
area = () => {
console.log("사각형의 넓이를 구합니다.");
}
}
var rec1 = new Rectangle(100, 200);
console.log(**rec1.#color**);Rectangle { width: 100, height: 200, area: [Function: area] }
class Rectangle {
**#color = "red";**
constructor(w, h) {
this.width = w;
this.height = h;
}
area = () => {
console.log("사각형의 넓이를 구합니다.");
}
}
var rec1 = new Rectangle(100, 200);
console.log(**rec1**);

| 종류 | 설명 |
|---|---|
| get | 접근자 프로퍼티를 통해 데이터 프로퍼티의 값을 가져와서 읽을 때 호출되는 접근자 함수 getter 함수가 호출되고 그 결과가 프로퍼티 값으로 반환 |
| set | 접근자 프로퍼티를 통해 데이터 프로퍼티의 값을 받아서 저장할 때 호출되는 접근자 함수 setter 함수가 호출되고 그 결과가 프로퍼티 값으로 저장 |
캡슐화한 프로퍼티에 접근할 수 있음
get 키워드로 선언
**get** display() {
return `가로는 ${this.width}, 세로는 ${this.height}입니다.`;
}
호출 방법
함수 호출 방식이 아닌 프로퍼티 호출 방식으로 호출
객체이름.display
캡슐화한 프로퍼티에 접근할 수 있음
set 키워드로 선언
**set** changeWidth(value) {
console.log(value);
}
값 저장 방법
값 할당 방식으로 저장
객체이름.changeWidth = 100;
class 클래스이름 extends 부모클래스이름 {
// 클래스 구현
}

코드 재사용 가능
sleep() {
super.sleep();
console.log('많이 잔다');
}
잔다. // 부모 클래스로부터 상속
많이 잔다. // 자식 클래스에서 추가
해당하면 true 리턴
해당하지 않으면 false 리턴
console.log(dog instanceof Animal); // true
console.log(cat instanceof Animal); // false