본문 바로가기

Programming/javascript

[javascript] prototype을 이용한 Map 효과

prototype을 이용한 Map 효과



<script>


var Map = function(){

this.obj = new Object();

};


Map.prototype = {

put : function(key, value){

this.obj[key] = value;

},

get : function(key){

return this.obj[key];

},

containsKey : function(key){

return key in this.obj;

},

containsValue: function (value) {

for(var key in this.obj){

if(value == this.obj[key]){

return true;

}

}

return false;

},

clear : function(){

for(var key in this.obj){

delete this.obj[key];

}

},

remove : function(key){

delete this.obj[key];

},

getKey : function(){

var arr = new Array();

for(var key in this.obj){

arr.push(key);

}

return arr;

},

getValues : function(){

var arr = new Array();

for(var value in this.obj){

arr.push(this.obj[value]);

}

return arr;

},

getSize : function(){

var count = 0;

for(var key in this.obj){

count++;

}

return count;

}

};


var map = new Map();


map.put("name", "seongilman");


// get

console.log("name : " + map.get("name"));


</script>