JS가 구현한 MAP

9585 단어 map
Array.prototype.remove = function(s) {
    for (var i = 0; i < this.length; i++) {
        if (s == this[i])
            this.splice(i, 1);
    }
}

/**
 * Simple Map
 * 
 * 
 * var m = new Map();
 * m.put('key','value');
 * ...
 * var s = "";
 * m.each(function(key,value,index){
 *         s += index+":"+ key+"="+value+"
"; * }); * alert(s); * * @author dewitt * @date 2008-05-24
*/ function Map() { /** ( ) */ this.keys = new Array(); /** */ this.data = new Object(); /** * * @param {String} key * @param {Object} value */ this.put = function(key, value) { if(this.data[key] == null){ this.keys.push(key); } this.data[key] = value; }; /** * * @param {String} key * @return {Object} value */ this.get = function(key) { return this.data[key]; }; /** * * @param {String} key */ this.remove = function(key) { this.keys.remove(key); this.data[key] = null; }; /** * Map, * * @param {Function} function(key,value,index){..} */ this.each = function(fn){ if(typeof fn != 'function'){ return; } var len = this.keys.length; for(var i=0;i<len;i++){ var k = this.keys[i]; fn(k,this.data[k],i); } }; /** * ( Java entrySet()) * @return {key,value} */ this.entrys = function() { var len = this.keys.length; var entrys = new Array(len); for (var i = 0; i < len; i++) { entrys[i] = { key : this.keys[i], value : this.data[i] }; } return entrys; }; /** * Map */ this.isEmpty = function() { return this.keys.length == 0; }; /** * */ this.size = function(){ return this.keys.length; }; /** * toString */ this.toString = function(){ var s = "{"; for(var i=0;i<this.keys.length;i++,s+=','){ var k = this.keys[i]; s += k+"="+this.data[k]; } s+="}"; return s; }; } function testMap(){ var m = new Map(); m.put('key1','Comtop'); m.put('key2',' '); m.put('key3',' '); alert("init:"+m); m.put('key1',' '); alert("set key1:"+m); m.remove("key2"); alert("remove key2: "+m); var s =""; m.each(function(key,value,index){ s += index+":"+ key+"="+value+"
"; }); alert(s); }

좋은 웹페이지 즐겨찾기