javascript Map

来源:互联网 发布:淘宝禁用词处罚 编辑:程序博客网 时间:2024/04/28 06:34
function pair(key, value) {
  
this.key = key;
  
this.value = value;
}

function put(key, value) {
  
for (var i = 0; i < this.map.length; i++) {
    
if (this.map[i].key == key) {
      
this.map[i].value = value;
      
return;
    }
  }
  
this.map[this.map.length] = new pair(key, value);
}

function get(key) {
  
for (var i = 0; i < this.map.length; i++) {
    
if (this.map[i].key == key)
      
return this.map[i].value;
  }
}

function remove(key) {
  
for (var i = 0; i < this.map.length; i++) {
    
if (this.map[i].key == key) {
      
for (var j = i; j < this.map.length - 1++j)
        
this.map[j] = this.map[j+1];
      
return;
    }
  }
}

function getCount(){
  
return this.map.length;
}

function isEmpty(){
  
return this.map.length == 0;
}

function classMap() {
  
this.map = new Array();
  
this.get = get;
  
this.put = put;
  
this.remove = remove;
  
this.getCount = getCount;
  
this.isEmpty = isEmpty;
}

 
原创粉丝点击