본문 바로가기
개발

Javascript - 프로토타입

by 플리트우드 2024. 12. 6.

메모리에 남아있는 원형 object. 인스턴스에 프로퍼티를 정의하면 날라가지만 프로토타입에 프로퍼티를 저장하면 얘를 상속한 다른 object들이 다 갖고있게 됨

출처 :  https://im-developer.tistory.com/98

 

 

function Bike(gears, startGear) {
  this.gears = gears;
  this.currentGear = startGear;
}
Bike.prototype.changeGear = function(direction, changeBy) {
  if (direction === 'up') {
    this.currentGear += changeBy;
  } else {
    this.currentGear -= changeBy;
  }
}


This way every object created from Bike inherits the changeGear function.

 

array의 프로토타입에 push가 있기때문에 arr.push(3)은 프로토타입의 함수를 갖다쓸수있음

'개발' 카테고리의 다른 글

네트워크 - DHCP  (0) 2024.12.06
IP주소, 서브넷 마스크  (1) 2024.12.06
Java - .java와 .class 파일, JVM  (0) 2024.12.06
코틀린 - 스코프 함수 let  (0) 2024.12.06
Kotlin - 코루틴  (0) 2024.12.06