프로토타입

1. 객체 생성 → 생성자 함수(원형)인 person 함수 생성

// 생성자 함수를 정의, 템플릿 역할
function person(name, age, department, univ) {
    this.name = name
    this.age = age
    this.department = department;
    ****this.univ = univ
}

Untitled

2. person 함수가 정의될 때 그 함수의 Prototype Object도 같이 생성 - 상호 연결

프로토타입 객체에는 다양한 속성과 메서드들이 정의되어 있음.

https://poiemaweb.com/js-prototype

https://poiemaweb.com/js-prototype

<aside> 💡 person → prototype prototype Object → constructor

prototype은 prototype Object 참조 constructor는 person 참조

</aside>

Untitled

3. 생성자 함수로 person1, person2 객체를 생성(클로닝, cloning)

// 생성자 함수를 사용하여 객체를 생성
var person1 = new person('ym', 26, '미소', '**sungkyul**');
var person2 = new person('hj', 26, '산경', '**sungkyul**');

<aside> 💡 person1, person2 → __proto __

</aside>

Untitled

공통적으로 가지고 있는 부분 - 메모리 차지 (univ)

4. 프로토타입 객체에 정의하여 속성과 메서드를 공유 - 메모리 효율성

person을 통해 생성된 person1, person2는 person의 프로토타입 객체에 정의된 속성과 메서드를 공유하여 사용할 수 있다.

생성자 함수로 객체를 생성할 때마다 해당 객체에 메서드를 복사하는 것이 아니라, 프로토타입 객체에 속성을 정의함으로써 메모리를 절약

모든 객체는 해당 생성자 함수의 프로토타입 객체를 참조하고 있으므로 메서드를 프로토타입에 정의하면 모든 객체에서 공유하여 사용 가능

**person.prototype.univ = 'sungkyul';**

person함수의 프로토타입 객체에 univ를 정의함