클로저를 통해 java의 getter setter를 만들 수 있다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function obj(){
var name = "홍길동";
//get
// return function(){
//return name;
// }
//set
//return function(aaa){
//name=aaa;
//}
return {
getName : function(){
return name;
},
setName : function(aaa){
name=aaa;
}
}
}
//get
//var getObj=obj();
//console.log(getObj());
//set
//var setObj=obj();
//setObj('이순신');
var result=obj();//obj반환
result.setName('이순신');//setter
console.log(result.getName());//getter
</script>
</body>
</html>
console.log(result);를 하면 obj가 출력됨.
이런 식으로
'JS' 카테고리의 다른 글
221229 JS JSON함수 (0) | 2022.12.29 |
---|---|
221228 JS 객체 (0) | 2022.12.28 |
221228 JS 클로저 (0) | 2022.12.28 |
221228 JS 전역변수 지역변수 (0) | 2022.12.28 |
221228 JS 함수의 가변인자 (0) | 2022.12.28 |