AJAX Widgets > DataMap 만들기
자바나 C 처럼 데이터를 객체에 저장하고 읽을 수 있는 Getter / Setter를 자바스크립트를 통해서도 간단히 만들 수 있다. 실제 이러한 방법은 어렵지 않지만, 그 동안 많이 사용하지 않았을 뿐이다. 간단히 이러한 기능을 할 수 있는 Widgets 코드를 소개하고자 한다.
Key-Value Pair 형식으로 데이터를 중복없이 저장하는 Map 개체
프로그램 구조
dataMap = function() { //--* Read this.getData = function(key) { } //--* Write this.setData = function(key, value) { } //--* Delete this.removeData = function(key) { } } ........HTML Usage............................. <script> var text= new dataMap(); text.setData("test","Test Data"); text.getData("test"); text.removeData("test"); </script>
dataMap = function() { this.data = new Object(); this.getData = function(key) { if(typeof(this.data[key]) != "undefined") { return this.data[key]; } } this.setData = function(key, value) { if(typeof(this.data[key])=="undefined") { var oTemp = new Object(); oTemp.key = key; oTemp.value = value; this.data[key] = oTemp; return true; } else { this.data[key].value = value; return true; } this.removeData = function(key) { if(typeof(this.data[key]) != "undefined") { delete(this.data[key]); return true; } else { return false; } } }
댓글 없음:
댓글 쓰기