const myObject = {
city: "Madrid",
greet() {
console.log(`Greetings from ${this.city}`);
},
};
myObject.greet(); // Greetings from Madrid
myObject를 city라는 프로퍼티와 greet라는 메소드를 가진 객체로 정의하였다.
만약 콘솔에 myObject. 을 입력한다면, 콘솔은 아래와 같이 해당 객체가 사용할 수 있는 프로퍼티와 메소드의 목록을 보여줄 것이다.
__defineGetter__
__defineSetter__
__lookupGetter__
__lookupSetter__
__proto__
city
constructor
greet
hasOwnProperty
isPrototypeOf
propertyIsEnumerable
toLocaleString
toString
valueOf
직접 정의한 city와 greet외에도 많은 프로퍼티를 가지고 있는 것을 확인할 수 있다.
null일때 종료되게 된다. (end of the chain)undefined를 반환한다__proto__ 프로퍼티로 접근하기. (즉 myObject.__proto__ 와 같은 식으로 사용)
prototype이 아니라 __proto__다! 헷갈리지 않도록 주의하자.Object.getPrototypeOf() 함수 사용하기
myObject의 프로토타입이 뭔지 확인해보자.Object.getPrototypeOf(myObject);

웬 객체가 나오는데, 내부를 살펴보면 constructor: f Object() 를 확인할 수 있다. 즉 Object.prototype 객체인 것이다.
Object.prototype 은 사실 가장 기본적인 프로토타입으로서, 모든 객체가 default로 가지고 있는 프로토타입이기도 하다. 가장 기본의 프로토타입이기 때문에, Object.prototype 의 프로토타입은 null이다.

그림으로 표현하면 이와 같다.
Object.prototype.__proto__와 Object.getPrototypeOf(Object.prototype)를 해보니 null이 나왔다!const myDate = new Date();
let object = myDate;
do {
object = Object.getPrototypeOf(object);
console.log(object);
} while (object);
// Date.prototype
// Object { }
// null
프로토타입 체인이 끝날때까지 체인을 탐색하는 코드이다.

myDate객체의 __proto__프로퍼티는 Date.prototype 객체를 가리키고, Date.prototype의 __proto__프로퍼티는 다시 Object.prototype을 가리키는 것이다.myDate.getMonth()와 같이 메소드를 호출하는 경우, 체인을 통해 Date.prototype에 정의된 메소드를 찾아 호출하는 것이다(myDate객체엔 getMonth 메소드가 정의되어 있지 않다)