prototype(원형: 객체들이

원기
– 사물 일반적으로 사용되는 속성 값

프로토타입을 사용하지 않는 생성자 함수 메서드를 바로 내부에 정의하면 얼마나 비효율적입니까?

– 객체가 생성될 때마다 메소드 중복으로 인한 메모리 쓰레기발생하다

프로토타이핑을 통해 이러한 비효율성을 어떻게 극복했나요?

– 사물 자주 사용되는 속성값 정의(prototype)따라서 메서드를 객체에 하나씩 쓰는 과정을 건너뛰어 효율적으로 메모리를 사용할 수 있다.

function Person(name,first,second){
    this.name =name;
    this.first=first;
    this.second=second;
    
}

//형태 <생성자함수.prototype.함수이름>
Person.prototype.sum=function(){
    return 'prototype : '+(this.first+this.second);
}
let kim = new Person('kim',10,20);
kim.sum = function(){
    return 'this : '+(this.first+this.second);
}
let lee = new Person('lee',10,10);


console.log("kim.sum()",kim.sum());// 먼저 자신(kim)이 sum이라는 객체를 가졌는지를 찾는다, 끝 
console.log("lee.sum()",lee.sum());// 만약 자신(lee)이 sum이라는 객체를 가지지 않는다면, prototype의 sum메소드를 따라간다,끝
//kim.sum() this : 30
//lee.sum() prototype : 20