用JavaScript实现map数据结构

前一段时间和同事在一起讨论,什么才能够改善我们的Javascrip代码。我当时的反应,就是“适合场景的数据结构,数据结构为王”,数据结构的设计优良要比代码的优良更好,没有很好的数据结构很难写出清晰、简洁的代码。
  当时就计划用Javascript实现几个常用的列表、二维数组、map、队列等高级数据结构,来作为以后改善javascript程序的基础。由于列表、队列、二维数组都可以用Array对象进行简单的模拟,在这里就不用赘述了!有兴趣的可以利用Array作为基础去实现上面几种数据结构!
  由于map结构,在我们的程序开发中很多场景都很需要,但是,我们现在的Javascrip设计,却总是使用循环来解决问题,所以就研究了很多prototype代码,吸取其使用Javascrip的精华,来实现这种特殊数据结构。后来,还是在刚开始学习Javascrip的一位大侠师傅身上,找到一个实现map的技术手段。
  下面将Javascrip实现的map数据结构V1.0,先行抛出,有问题大家一起讨论!自己也经过简单的测试,还是有一点点小问题的,特别在应对key和value都是比较大的数字时!
------------------------------------------------
function Map(){
   this.map    =  new Object();
   this.length = 0;
  
   this.size = function(){
       return this.length;
   }
  
   this.put = function(key, value){
     
      if( !this.map['_' + key])
        {
             ++this.length;
        }
     
      this.map['_' + key] = value;
    
   }
  
   this.remove = function(key){
      
       if(this.map['_' + key])
      {
         
          --this.length;
        return delete this.map['_' + key];
      }
      else
      {
          return false;
      }
   }
  
   this.containsKey = function(key){
   
     return this.map['_' + key] ? true:false;
  
   }
   
   this.get = function(key){   
 
      return this.map['_' + key] ? this.map['_' + key]:null;
 
   }


 this.inspect=function(){   
     var str = '';
    
     for(var each in this.map)
     {
          str+= '/n'+ each + '  Value:'+ this.map[each];
     }
    
     return str;
   }
    
}
------------------------------------------
测试程序

//测试Map的调用方法

function testMap(){
   var testmap=new Map();
   alert("cur size:" + testmap.size());
    alert("get bu cunzai:" + testmap.get("bu cunzai"));
   testmap.put("01","michael");
    testmap.put("01","michael1");
     testmap.put("01","michael11");
      testmap.put("01","michael1111");
       testmap.put("011","michael1111");
        testmap.put("object",new Array());
        testmap.put("number",1234);
        testmap.put(78944444444444444422222222222222,1234);
        testmap.put("function",function(num){return num;});
      
        //alert("function:"+ testmap.get("function")(789));
        alert("function:"+ testmap.get("function")(78944444444444444422222222222222));
   alert ("cur size:" + testmap.size() + "inspect:" + testmap.inspect());
    alert("remove function:" + testmap.remove("function"));
    alert("remove object:" + testmap.remove("object"));
   testmap.put("02","michael2");
   testmap.put("022","achang2");
   testmap.put("022","achang3");
   alert ("cur size:" + testmap.size() + "inspect:" + testmap.inspect());
  
 
   var key="02"
   
   if (testmap.containsKey(key)){
       var value=testmap.get(key);
       alert ("02 first|"+value);
   }else{
       alert("no Cotain" + key);
   }
 
  alert("remove:" + testmap.remove("02"));
  alert("cur size:" + testmap.size());
  alert( testmap.remove("0000002"));
  alert("cur size:" + testmap.size());
  alert("contain:" + testmap.containsKey("0000002"));
  
  if (testmap.containsKey(key)){
       var value=testmap.get(key);
       alert ("02 |"+ value);
   }else{
       alert ("no Contain:"+key);
   }
 
  

}
testMap();
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值