javascript中的Map实现

  1. var Map = function(){  
  2.     // 构造entry实体   
  3.     var Entry = function(key, value){  
  4.         this.key = key;  
  5.         this.value = value;  
  6.     }  
  7.       
  8.     this.entries = new Array();  
  9.     // 构造put方法在数组中放入一个Entry   
  10.     this.put = function(key, value){  
  11.         // 数组中已存在就不放入   
  12.         for (var i = 0; i < this.size(); i++) {  
  13.             if (this.entries[i].key === key) {  
  14.                 return false;  
  15.             }  
  16.         }  
  17.         this.entries.push(new Entry(key, value));  
  18.     };  
  19.     // 模拟get方法   
  20.     this.get = function(key){  
  21.         for (var i = 0; i < this.size(); i++) {  
  22.             if (this.entries[i].key === key) {  
  23.                 return this.entries[i].value;  
  24.             }  
  25.         }  
  26.         return null;  
  27.     };  
  28.       
  29.     // 查找下标值   
  30.     this.indexOf = function(key){  
  31.         var index = -1;  
  32.         for (var i = 0; i < this.size(); i++) {  
  33.             if (this.entries[i].key === key) {  
  34.                 index = i;  
  35.                 break;  
  36.             }  
  37.         }  
  38.         return index;  
  39.     }  
  40.     // 删除一个元素   
  41.     this.remove = function(key){  
  42.         var index = this.indexOf(key);  
  43.         if (index != -1) {  
  44.             this.entries.splice(index, 1);  
  45.         }  
  46.     }  
  47.     // 取得map长度   
  48.     this.size = function(){  
  49.         return this.entries.length;  
  50.     };  
  51.       
  52.     // 重新设置键值对   
  53.     this.setValue = function(key, value){  
  54.         var index = this.indexOf(key);  
  55.         if (index != -1) {  
  56.             this.entries[i].value = value;  
  57.         };  
  58.     };  
  59.       
  60.     // 是否为空map   
  61.     this.isEmpty = function(){  
  62.         return this.size() <= 0;  
  63.     };  
  64.       
  65.     //清空map ;   
  66.     this.clear = function(){  
  67.         this.entries = [];  
  68.     };  
  69.       
  70.     // 得到entry实体   
  71.     this.getEntry = function(index){  
  72.         if (index >= 0 && index < this.size()) {  
  73.             return this.entries[index];  
  74.         }  
  75.         return null;  
  76.     }  
  77.       
  78.     this.toString = function(){  
  79.         var str = "[";  
  80.         for (var i = 0; i < this.size(); i++) {  
  81.             str += this.getEntry(i).key + "=" + this.getEntry(i).value + ",";  
  82.         }  
  83.         // 去除最后一个","   
  84.         str = str.substring(0, str.length - 1);  
  85.         str += "]";  
  86.         return str;  
  87.     };  
  88. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值