아래 스크립트대로 쓰는 게 왜 유용한 건지?
// 출처는 https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
function Person(first, last) {
this.first = first;
this.last = last;
}
Person.prototype.fullName = function() {
return this.first + ' ' + this.last;
};
Person.prototype.fullNameReversed = function() {
return this.last + ', ' + this.first;
};
객체 속 프로퍼티가 각 2개일 때 총 객체를 5개 선언하면 프로퍼티에 할당되는 메모리 공간은 모두 10개임. 이때 프로퍼티가 상수일 때 메모리 공간을 10개나 할당할 필요 없이 2개만 할당하고 객체들이 이 공간을 공유하여 쓸 수 있는 게 프로토타입.
기본 속성으로 __proto__
와 constructor
을 가지고 있음.
함수를 정의할 때 함수와 prototype object도 생성이 됨.
이때 생성된 함수의 prototype
속성을 통해 prototype object에 접근
객체가 생성될 때 모든 객체의 기본 속성인 __proto__
은 생성된 객체의 조상인 prototype object를 가리킨다.
객체가 속성을 직접 가지고 있지 않는 경우 객체의 조상인 prototype object에서 탐색하고 여기에도 없으면 최상위까지 탐색한다. 그럼에도 못 찾는 경우, undefined를 리턴한다.
(프로토타입 체인 구조)
this는 기본적으로 window,
이거 분석하기
https://stackoverflow.com/questions/111102/how-do-javascript-closures-work