[JS] 프로토타입과 클래스

홍다희·2022년 7월 22일
0

JavaScript는 프로토타입(원형 객체) 기반 언어다.

class Human {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  sleep() {
    console.log(`${this.name}은 잠에 들었습니다`);
  }
}

let kimcoding = new Human('김코딩', 30);

Human.prototype.constructor === Human; // true
Human.prototype === kimcoding.__proto__; //true
Human.prototype.sleep === kimcoding.sleep; //true

Human이라는 클래스와 인스턴스, 그리고 프로토타입의 관계

Array(배열) 클래스와 인스턴스, 그리고 프로토타입의 관계

모르고 사용해 왔지만 Array도 클래스 중 하나다. 지금까지 써왔던 배열은 Array 클래스의 인스턴스이며, Array.prototype에는 push, pop 등 다양한 메서드가 있다.

profile
프론트엔드 개발자

0개의 댓글