Prototype

자바스크립트의 Prototype 이란, 어떠한 객체가 만들어지기 위한 그 객체의 모태(원형)를 말한다. 자바스크립트의 모든 객체는 Object 객체의 프로토타입을 기반으로 확장 되었다.

Prototype은 크게 두가지 특징을 가지고 있다.

  1. 생성자 (constructor) 와 쌍을 이룬다.
  2. Instance 와 연결되어 있다. (Dunder proto와 prototype chain)

첫번째, 생성자 (constructor) 와 쌍을 이룬다.

마치 “부부”처럼, 생성자가 있으면 Prototype은 반드시 존재한다. 생성자에는 아내인 Prototype에 대한 정보를 “기본적”으로 가지고 있다. 그 정보(prototype의 reference)를 prototype 이라는 속성에 저장 한다. 반대로 아내 prototype도 남편의 정보(constructor의 reference)를 constructor라는 속성에 저장하고 있다.

아래 이미지에서 파란색 줄은 Object라는 생성자의 prototype 속성에 저장된 정보를, 노란색 줄은 생성자 아내(prototype)의 constructor 속성에 저장된 정보를 보여준다.

각각의 속성의 값들은 그 자체로 아내(prototype)와 남편(constructor)을 가리킨다.

Object Constructor - 생성자로 객체 만들기

객체 생성자(Object Constructor)는 비슷한 객체를 무수히 만들어낼 수 있다.

cf. 객체 리터럴로 객체를 생성할 수도 있다. var dog = { name: ‘메시’, breed:’푸들’, weight: ‘6kg’}

객체 생성자 만들기

1
2
3
4
5
6
7
function Dog(name, breed, weight) {
//객체 생성자 특징 1. a function, 2.name with Upper Case
this.name = name; //3. 'this' Keywords
this.breed = breed;
this.weight = weight;
//4. return nothing
}

객체 생성자 특징

  1. 객체 생성자는 함수이다.
  2. 생성자 함수명은 일반적으로 대문자로 시작한다.
  3. this 키워드를 사용한다.
  4. 생성자 함수는 아무것도 반환하지 않는다.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×