[JavaScript] Prototype의 모던한 적용방식

·2022년 2월 4일

JavaScript

목록 보기
9/9
post-thumbnail

ProtoType 설명 여기서 잠깐 써뒀지만, 이건 다소 구식이라고 한다.

Modern Prototype

Object.create(proto, [description]);
Object.getPrototypeOf(obj);
Object.setPrototypeOf(obj, proto);
  1. Object.create(proto, [description])
    • [[Prototype]]이 proto인 Object를 만든다.
  2. Object.getPrototypeOf(obj)
    • obj의 [[Prototype]]을 반환한다.
  3. Object.setPrototypeOf(obj, proto)
    • obj의 [[Prototype]]을 proto로 설정한다.

1번 예시

let animal = {
	eats: true
};

let rabbit = Object.create(animal);

이제 rabbit의 [[Prototype]]은 animal이다.

2번 예시

let animal = {
	eats: true
};

let rabbit = Object.create(animal);

alert(Object.getPropertyOf(rabbit) == animal) // true

[[Propertype]]을 서로 비교해봐도 알 수 있다.

3번 예시

let animal = {
	eats: true
};

let rabbit = Object.create(animal);

Object.setPropertyOf(rabbit, {})

이제 rabbit의 property는 빈 객체이다.

profile
어?머지?

0개의 댓글