源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

javascript实现的HashMap类代码

  • 时间:2020-03-22 11:05 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:javascript实现的HashMap类代码
[u]复制代码[/u] 代码如下:
<script language = "javascript" > function HashMap() {     /**Map大小**/     var size = 0;     /**对象**/     var entry = new Object();     /**Map的存put方法**/     this.put = function(key, value) {         if (!this.containsKey(key)) {             size++;             entry[key] = value;         }     }     /**Map取get方法**/     this.get = function(key) {         return this.containsKey(key) ? entry[key] : null;     }     /**Map删除remove方法**/     this.remove = function(key) {         if (this.containsKey(key) && (delete entry[key])) {             size--;         }     }     /**是否包含Key**/     this.containsKey = function(key) {         return (key in entry);     }     /**是否包含Value**/     this.containsValue = function(value) {         for (var prop in entry) {             if (entry[prop] == value) {                 return true;             }         }         return false;     }     /**所有的Value**/     this.values = function() {         var values = new Array();         for (var prop in entry) {             values.push(entry[prop]);         }         return values;     }     /**所有的 Key**/     this.keys = function() {         var keys = new Array();         for (var prop in entry) {             keys.push(prop);         }         return keys;     }     /**Map size**/     this.size = function() {         return size;     }     /**清空Map**/     this.clear = function() {         size = 0;         entry = new Object();     } } //创建HashMap对象 var hashMap = new HashMap(); hashMap.put("A", "1"); hashMap.put("B", "2"); hashMap.put("A", "5"); hashMap.put("C", "3"); hashMap.put("A", "4"); alert(hashMap.size()); </script>
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部