用js模拟的Map

来源:互联网 发布:只有我知350分钟 编辑:程序博客网 时间:2024/05/17 23:22
/* * 将键映射到值的对象。 * 一个映射不能包含重复的键;每个键最多只能映射到一个值。  *  author: 吴安国 * version: 1.0 */function Map() {this.count = 0;this.keys = new Array();};Map.prototype = new Array();/** * 将指定的值与此映射中的指定键关联 * @param key  键 * @param value 值 */Map.prototype.put = function(key, value) {this[key] = value;this.count ++;this.keys[this.keys.length] = key;};/** *  获取指定键的值 * @param key  键 * @return 值 */Map.prototype.get = function(key) {return this[key];};/** * 清空键值 */Map.prototype.clear = function() {for(var i = 0; i < this.count; i++) {delete this[this.keys[i]];}this.keys = new Array();this.count = 0;};/** * 如果此映射未包含键-值映射关系,则返回 true。 * @return true or false */Map.prototype.isEmpty = function() {return this.count == 0;};/** * 如果此映射将一个或多个键映射到指定值,则返回 true。 * @param key 键 * @return true or false */Map.prototype.containsKey = function(key) {return this[key] != undefined;};/** * 如果存在一个键的映射关系,则将其从此映射中移除。 * @param key 键 * @return 值 */Map.prototype.remove = function(key) {var value = this[key];delete this[key];this.count --;return value;};/** *  返回此映射中的键-值映射关系数。 * @return 关系数 */Map.prototype.size = function() {return this.count;};


 

原创粉丝点击