자바스크립트에서 일반적으로 function 으로 선언하는 함수는 객체입니다.
비록 완벽하게는 아니지만 어느정도 OOP가 가능하다는 말이죠
예를 들면
function AAA()
{
this.width = 3;
this.height = 4;
}
이런식으로 선언하고
var objAAA = new AAA();
document.write( objAAA.width + " " + objAAA.height );
하시면 객체의 프로퍼티르르 지정할 수 있습니다.
그럼 이번엔 메소드를 추가 해 보죠
function AAA()
{
this.width = 3;
this.height = 4;
this.getArea = function()
{
return this.width * this.height;
}
}
var objAAA = new AAA();
alert( objAAA.getArea());
이렇게 하면 AAA라는 객체에 getArea라는 메소를 추가하게 됩니다.
하지만 이렇게 하면 단점이 하나 있습니다.
바로 AAA객체를 생성할 때 마다 getArea라는 객체를 또 생성하게 된다는 것이죠
이를 보완하기 위해 나온것이 prototype입니다
위 코드를 prototype을 적용시키면 다음과 같습니다
function AAA()
{
this.width = 3;
this.height = 4;
}
AAA.prototype.getArea = function()
{
return this.width * this.height;
}
var objAAA = new AAA();
alert( objAAA.getArea());
출처:http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040202&docId=62222679&qb=cHJvdG90eXBl&enc=utf8§ion=kin&rank=1&sort=0&spq=1&pid=f105twoi5TosscXKVZNsss--475676&sid=S1znUprRXEsAABd6G8M