Javascript&Jquery
게터와 세터
알 수 없는 사용자
2012. 1. 23. 18:12
//생성자 함수를 선언
function Rectangle(w,h) {
//변수를 선언
var width=w;
var height=h;
//매서드를 선언합니다.
this.getWidth=function(){return width;};
this.getHeight=function(){return height;};
this.setWidth=function(value) {
if(value<0){
throw '길이는 음수일 수 없습니다.';
} else {
width =value;
}
};
this.setHeight=function(value) {
if (value<0) {
throw '길이는 음수일 수 없습니다.';
} else{
height=value;
}
};
}
Rectangle.prototype.getArea=function(){
return this.getWidth()*this.getHeight();
};
//변수를 선언
var rectangle = new Rectangle(5,7);
rectangle.setWidth(2);
//출력
alert('AREA:'+rectangle.getArea());
width와 height에 음수가 들어가면 작동되지 않도록 throw 키워드를 실행
캡슐화: 만일의 상황에 대비해서 특정속성이나 매서드를 사용자가 사용할수 없게 숨겨놓은것
width와 height에 음수가 들어가면 작동되지 않도록 throw 키워드를 실행
캡슐화: 만일의 상황에 대비해서 특정속성이나 매서드를 사용자가 사용할수 없게 숨겨놓은것