当前位置: 代码迷 >> JavaScript >> 用javascript打包的一个HashMap,形似神非
  详细解决方案

用javascript打包的一个HashMap,形似神非

热度:207   发布时间:2012-10-25 10:58:57.0
用javascript封装的一个HashMap,形似神非
function HashMap(){
            this.size=0;
            this.map=new Object();
        }
        
        HashMap.prototype.put=function(key,value){
            if(!this.map[key]){
                this.size++;
            }
            this.map[key]=value;
        }
        HashMap.prototype.get=function(key){
            return this.isKey(key)?this.map[key]:null;
        }
        HashMap.prototype.isKey=function(key){
            return (key in this.map);
        }
        HashMap.prototype.remove=function(key){
          if( this.isKey(key) && (delete this.map[key])){  
                this.size--;  
          }  
        }
        
        HashMap.prototype.size=function(){
            return this.size;
        }
        
        HashMap.prototype.find=function(_callback){
            for(var _key in this.map){
                _callback.call(this,_key,this.map[_key]);
            }
        }
function testHashMap(){
            var map=new HashMap();
            map.put("a","中");
            map.put("b","国");
            map.put("c","人");
            map.put("d","民");
            
            //map.remove("a");
            map.remove("b");
            map.find(function(key,value){
                map.remove(key);
            });
            alert(map.size());
        }

?

  相关解决方案