1. Prototypes and the prototype chain

const myObject = {
  city: "Madrid",
  greet() {
    console.log(`Greetings from ${this.city}`);
  },
};

myObject.greet(); // Greetings from Madrid

프로토타입과 프로토타입 체인

객체의 프로토타입에 접근하는 방법

  1. (deperecated) __proto__ 프로퍼티로 접근하기. (즉 myObject.__proto__ 와 같은 식으로 사용)
  2. 더 공식적인 방법 - Object.getPrototypeOf() 함수 사용하기

그럼 실제로 위 코드의 myObject의 프로토타입이 뭔지 확인해보자.

Object.getPrototypeOf(myObject);

Untitled

당연히 모든 객체의 프로토타입이 Object.prototype인 것은 아니다. (다만 단군할아버지가 항상 Object.prototype인건 맞는거겠지??) + 프로토타입 체인 예시

const myDate = new Date();
let object = myDate;

do {
  object = Object.getPrototypeOf(object);
  console.log(object);
} while (object);

// Date.prototype
// Object { }
// null