<생성자>
다른 언어에서의 class
대문자로 시작

function Calc(first, second) {
  this.first = first;
  this.second = second;
  this.add = function () {
    return this.first + this.second;
  }
}

this.add를 생성자 함수 안에 넣는 대신에 이렇게 할 수도 있다.

Calc.prototype.add = function () {
  return this.first + this.second;
}

위처럼 prototype으로 공통된 함수를 정의하면 메모리를 아낄 수 있다.
prototype은 생성자 함수에 사용자가 직접 넣는 것이고, __proto__는 new를 호출 할 때 prototype을 참조해 자동으로 만들어지는 객체

+ Recent posts