• 정의: Blueprint -> Class
  • 활용: Object -> Instance
  • 클래스 함수로 정의 가능
function Car(brand, name, color) {
  // 인스턴스 생성시 실행되는 코드
}
  • 클래스의 속성 정의
function Car(brand, name, color) {
  this.brand = brand;
  this.name = name;
  this.color = color;
}
  • 클래스의 메소드 정의
function Car(brand, name, color) { 
  // 생략 
}
Car.prototype.refuel = function() {
  // 연료 재공급 구현 코드
}
Car.prototype.drive = function() {
  // 운전 구현 코드
}

prototype 은 모델의 Blueprint를 만들 때 사용되는 원형 객체입니다. 그리고 this"함수가 실행될 때, 해당 scope마다 생성되는 고유한 실행 컨텍스트(Execution Context)"로써 " new 키워드로 인스턴스가 생성되었을 때, 해당 인스턴스(Instance)가 바로 this의 값이 됩니다.

코드 및 자료 출처: 코드스테이츠(CodeStates)

0개의 댓글