javascript 캡슐화 하는 방법(클로저 이용)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
Remove this if you use the .htaccess -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>day07_capsulaition</title>
<meta name="description" content="">
<meta name="author" content="01-717-08">
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<link rel="shortcut icon" href="/favicon.ico">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<script type = "text/javascript">
<!--
function Rectangle(width, height){
/*
this.width=width;
this.height=height;
이렇게 하면 캡슐화가 안된다. 그래서*/
var w = width; //지역변수 : 외부접근불가
var h = height;
this.getArea= function(){ //inner function
var result =w*h;
alert('Area:' + result);
};
}
//-->
var robj = new Rectangle(4,5); // 여기를 실행하고 나면 var들로 정의되어 있기에
// 메모리에서 사라지게 되지만
robj.getArea(); // 이부분에서 getArea() 에서 값이 불러와 진다. 말이 안되는데
// 이게 클로저라는게 되있기 떄문에 가능함
</script>
</head>
<body>
</body>
</html>