자바스크립트 - 캡슐화(Encapsulation)

CODEDRAGON Development/JavaScript, jQuery, ...

반응형


 

 

캡슐화(Encapsulation)

객체 내부에 데이터가 저장되는 영역을 지역변수화해서 객체 외부에서 직접 접근을 불허하고 메서드를 이용해 접근하도록 처리합니다.

 

// 생성자 함수를 선언

function Rectangle(w, h) {

 

var width = w; // 지역변수

var height = h; // 지역변수

 

this.getWidth = function() {

return width;

};

this.getHeight = function() {

return height;

};

this.setWidth = function(w) {

width = w;

};

this.setHeight = function(h) {

height = h;

};

}

 

 

 



반응형