js簡單模仿HashMap

看了很多ECMAScript,心血來潮寫了個簡單的模仿JAVA中HashMap的js對象,對泛型簡單的實現java

//用閉包模式把對象定義到Window上,避免屬性名稱的值被篡改
(function(win){
    var HashMap = function(keyType,valueType){
        this.length = 0 ;
        this._keys = [];
        this._values = [];
        this.size = function(){
            return this.length;
        };
        this.put = function(key,value){
            if(!keyType && !valueType){  //對沒有定義存儲類型的Map
                   for(var i in this._keys){
                        if(this._keys[i] == key){
                            this._values[i] = value;
                            return;
                        }
                    }
                    this._keys.push(key);
                    this._values.push(value);
                    this.length++;
            }else{  //定義了類型的 new HashMap("string",Object);
                if(typeof(key) == keyType && (value instanceof valueType)){
                    for(var i in this._keys){
                        if(this._keys[i] == key){
                            this._values[i] = value;
                            return;
                        }
                    }
                    this._keys.push(key);
                    this._values.push(value);
                    this.length++;
                }else{
                    alert("對象類型不符合");
                }
            }
        };
        this.get = function(key){
             for(var i in this._keys){
                  if(this._keys[i] == key){
                      return this._values[i];
                 }
              }
              return null;
        };
        
        this.remove = function(key){
             for(var i in this._keys){
                  if(this._keys[i] == key){
                      var temp = this._values[i];
                      //刪除key  
                      this._keys = this._keys.slice(0,i).concat(this._keys.slice(++i));
                      //刪除key對應的value
                      this._values = this._keys.slice(0,--i).concat(this._keys.slice(++i));
                 
                      this.length--;
                      return temp;
                 }
              }
              return null;
        };
        
        this.isEmpty = function(){
            return this.length == 0;
        };
        
        this.clear = function(){
            this.length = 0;
            this._keys = [];
            this._values = [];
        };
        this.putAllMap = function(map){
            if(map instanceof HashMap){
                this._keys.concat(map._keys);
                this._values.concat(map._values);
                this.length += map.length;
            }else{
                 throw new Error("不爲HashMap對象")
            }
        };
        
        this.containsKey = function(key){
            for(var i in this._keys){
                if(this._keys[i] == key){
                    return true;
                }
            }
            return false;
        };
        
        this.keysClone  = function(){
            var keys = [];
            for(var i in this._keys){
                keys.push(this._keys[i]);
            }
            return keys;
        };
        this.toString = function(){ //此方法要求存儲對象實現了toString()方法
            var data = "{";
            for(var i in this._keys){
                data += this._keys[i] +":" + this._values[i].toString() +","; 
            }
            data = data.substring(0,data.length-1)+"}";
            return data;
        }
    }
    win.HashMap = HashMap;
})(window);

function Dog(name){
    this.name = name;
    this.toString = function(){
        return this.name;
    }
}

//使用

var map = new HashMap('string',Dog); //此map只存放  key爲stirng類型,value是用new Dog("")生產的對象
map.put("傑克",new Dog("傑克"));
map.put("湯姆",new Dog("湯姆"));
map.put("大神",new Dog("大神"));
map.put("小仙",new Dog("小仙"));

alert(map.remove("大神").toString());

java中Map不少方法沒實現,還須要補充。。。。待續閉包

相關文章
相關標籤/搜索