본문 바로가기
Web/Javascript

[Javascript 기본 개념] Javascript 문법-프로토타입(prototype)

by 나비와꽃기린 2016. 6. 23.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1.1  prototype


한국어로는 원형정도로 번역되는 prototype은 말 그대로 객체의 원형이라고 할 수 있다

prototype에 저장된 속성들은 생성자를 통해서 객체가 만들어질 때 그 객체에 연결된다.

Ex)

function Ultra(){}

Ultra.prototype.ultraProp = true;

function Super(){}

Super.prototype = new Ultra();

function Sub(){}

Sub.prototype = new Super();

var o = new Sub();

console.log(o.ultraProp);

 

à prototype 체인으로 Sub Ultra 연결되어 있기 때문에 생성자 Sub 통해서 ultraProp 접근 가능하다.

à 수행 과정

  1. 객체 o에서 ultraProp 찾는다.
  2. 없다면 Sub.prototype.ultraProp 찾는다.
  3. 없다면 Super.prototype.ultraProp 찾는다.
  4. 없다면 Ultra.prototype.ultraProp 찾는다.

 

 

*prototype는 객체와 객체를 연결하는 체인의 역할을 하는 것이다. 이러한 관계를 prototype chain이라고 한다.