本程序用JS写的一个类拟于JAVA中MAP类,可以对键值对进行维护.


/*
name:    Map.js
author:  WindDC
date:    2006-10-27
content: 本程序用JS实现类拟JAVA中MAP对像的功能
*/

function Node(key,value){//键值对对象
    this.key=key;
    this.value=value;
}

function Map(){//Map类
    this.nodes=new Array();
}

Map.prototype.put=function(key,value){//往容器中加入一个键值对
        for(var i=0;i<this.nodes.length;i++)
           if(this.nodes[i].key==key){//如果键值已存在,则put方法为更新已有数据
               this.nodes[i].value=value;
               return;
           }
        var node=new Node(key,value);
        this.nodes.push(node);
        return;
}//put

  
Map.prototype.get=function(key){//获取指定键的值
        for(var i=0;i<this.nodes.length;i++)
           if(this.nodes[i].key==key)
              return this.nodes[i].value;
        return null;
}//get
    
Map.prototype.size=function(){//获取容器中对象的个数
     return this.nodes.length;
}//size

        
Map.prototype.clear=function(){//清空容器
     while(this.nodes.length>0)
        this.nodes.pop();     
}//clear
 
Map.prototype.remove=function(key){//删除指定值
     for(var i=0;i<this.nodes.length;i++)
        if(this.nodes[i].key==key){
           if(i>0)
              var nodes1=this.nodes.concat(this.nodes.slice(0,i-1),this.nodes.slice(i+1));
           else//删除的是第一个元素
             var nodes1=nodes.slice(1);
           this.nodes=nodes1;

        }
}//remove

   
Map.prototype.isEmpty=function(){//是否为空
     if(this.nodes.length==0)
       return true;
     else
       return false;
}//isEmpty
   
Map.prototype.toString=function(){
     var str="[";
     for(var i=0;i<this.nodes.length;i++){
        if(i<this.nodes.length-1)
           str=str+this.nodes[i].key+",";
       else
           str=str+this.nodes[i].key;    
    }
    str=str+"]";
    return str;
}

posted on 2006-12-01 17:24 WindDC 阅读(1471) 评论(6)  编辑  收藏 所属分类: js/ajax
Comments
  • # re: 用JS实现的MAP类
    BeanSoft
    Posted @ 2006-12-05 13:24
    赞一个!  回复  更多评论   
  • # re: 用JS实现的MAP类
    胡晓光
    Posted @ 2008-11-04 13:46
    Map 里面竟然是个Array,似乎不太好,直接用原型做个Map不就行了么,而且删除值的时候还要循环遍历Array
    贴出我的方案:
    function HashMap(){
    this.length = 0;
    this.container = {};
    }

    HashMap.prototype.put = function(objName,objValue){
    try{
    if(objValue && objName && objName != ""){
    this.container[objName] = objValue;
    this.length ++ ;
    }
    }catch(e){
    return e;
    }
    };

    HashMap.prototype.get = function(objName){
    try{
    if(this.container[objName])
    return this.container[objName];
    }catch(e){
    return e;
    }
    };

    HashMap.prototype.contain = function(objValue){
    try{
    for(var p in this.container){
    if(this.container[p] === objValue)
    return true;
    }
    return false;
    }catch(e){
    return e;
    }
    };

    HashMap.prototype.remove = function(objName){
    try{
    if(this.container[objName]){
    delete this.container[objName];
    this.length -- ;
    }
    }catch(e){
    return e;
    }
    };

    HashMap.prototype.pop = function(objName){
    try{
    var ov = this.container[objName];
    if(ov){
    delete this.container[objName];
    this.length -- ;
    return ov;
    }
    return null;
    }catch(e){
    return e;
    }
    };

    HashMap.prototype.removeAll = function(){
    try{
    this.clear();
    }catch(e){
    return e;
    }
    };

    HashMap.prototype.clear = function(){
    try{
    delete this.container;
    this.container = {};
    this.length = 0;
    }catch(e){
    return e;
    }
    };

    HashMap.prototype.isEmpty = function(){
    if(this.length === 0)
    return true;
    else
    return false;
    };

    HashMap.prototype.runIn = function(fun){
    try{
    if(!fun)
    throw new Error("未定义处理函数");
    for(var p in this.container){
    var ov = this.container[p];
    fun(ov);
    }
    }catch(e){
    return e;
    }
    };  回复  更多评论   
  • # re: 用JS实现的MAP类
    飞影
    Posted @ 2009-01-14 11:41
    发现这份代码被拷贝了好多次啊。
    错也是一样的拷贝,不知道楼上的有没有仔细看过啊?

    if(this.length === 0) 三个等号。。。。  回复  更多评论   
  • # 一楼的remove方法有误,正确的如下
    Xwei
    Posted @ 2009-02-20 16:28
    Map.prototype.remove=function(key){//删除指定值
    for(var i=0;i<this.nodes.length;i++)
    if(this.nodes[i].key==key){
    if(i>0){
    var nodes1=(this.nodes.slice(0,i)).concat(this.nodes.slice(i+1,this.nodes.length));
    }else{//删除的是第一个元素
    var nodes1=this.nodes.slice(1);
    }
    this.nodes=nodes1;
    break;
    }
    }//remove  回复  更多评论   
  • # 刚才说错了,应该是楼主的remove方法有误,正确的如下
    Xwei
    Posted @ 2009-02-20 16:29
    Map.prototype.remove1=function(key){//删除指定值
    for(var i=0;i<this.nodes.length;i++)
    if(this.nodes[i].key==key){
    if(i>0){
    var nodes1=(this.nodes.slice(0,i)).concat(this.nodes.slice(i+1,this.nodes.length));
    }else{//删除的是第一个元素
    var nodes1=this.nodes.slice(1);
    }
    this.nodes=nodes1;
    break;
    }
    }//remove  回复  更多评论   
  • # ...
    Xwei
    Posted @ 2009-02-20 16:30
    Map.prototype.remove=function(key){//删除指定值
    for(var i=0;i<this.nodes.length;i++)
    if(this.nodes[i].key==key){
    if(i>0){
    var nodes1=(this.nodes.slice(0,i)).concat(this.nodes.slice(i+1,this.nodes.length));
    }else{//删除的是第一个元素
    var nodes1=this.nodes.slice(1);
    }
    this.nodes=nodes1;
    break;
    }
    }//remove  回复  更多评论   

只有注册用户登录后才能发表评论。


网站导航: