• 一个简单的JavaScript Map


    用js写了一个Map,带遍历功能,请大家点评下啦。

    //map.js

    Array.prototype.remove = function(s) {
    	for (var i = 0; i < this.length; i++) {
    		if (s == this[i])
    			this.splice(i, 1);
    	}
    }
    
    /**
     * Simple Map
     * 
     * 
     * var m = new Map();
     * m.put('key','value');
     * ...
     * var s = "";
     * m.each(function(key,value,index){
     * 		s += index+":"+ key+"="+value+"
    ";
     * });
     * alert(s);
     * 
     * @author dewitt
     * @date 2008-05-24
     */
    function Map() {
    	/** 存放键的数组(遍历用到) */
    	this.keys = new Array();
    	/** 存放数据 */
    	this.data = new Object();
    	
    	/**
    	 * 放入一个键值对
    	 * @param {String} key
    	 * @param {Object} value
    	 */
    	this.put = function(key, value) {
    		if(this.data[key] == null){
    			this.keys.push(key);
    		}
    		this.data[key] = value;
    	};
    	
    	/**
    	 * 获取某键对应的值
    	 * @param {String} key
    	 * @return {Object} value
    	 */
    	this.get = function(key) {
    		return this.data[key];
    	};
    	
    	/**
    	 * 删除一个键值对
    	 * @param {String} key
    	 */
    	this.remove = function(key) {
    		this.keys.remove(key);
    		this.data[key] = null;
    	};
    	
    	/**
    	 * 遍历Map,执行处理函数
    	 * 
    	 * @param {Function} 回调函数 function(key,value,index){..}
    	 */
    	this.each = function(fn){
    		if(typeof fn != 'function'){
    			return;
    		}
    		var len = this.keys.length;
    		for(var i=0;i<len;i++){
    			var k = this.keys[i];
    			fn(k,this.data[k],i);
    		}
    	};
    	
    	/**
    	 * 获取键值数组(类似Java的entrySet())
    	 * @return 键值对象{key,value}的数组
    	 */
    	this.entrys = function() {
    		var len = this.keys.length;
    		var entrys = new Array(len);
    		for (var i = 0; i < len; i++) {
    			entrys[i] = {
    				key : this.keys[i],
    				value : this.data[i]
    			};
    		}
    		return entrys;
    	};
    	
    	/**
    	 * 判断Map是否为空
    	 */
    	this.isEmpty = function() {
    		return this.keys.length == 0;
    	};
    	
    	/**
    	 * 获取键值对数量
    	 */
    	this.size = function(){
    		return this.keys.length;
    	};
    	
    	/**
    	 * 重写toString 
    	 */
    	this.toString = function(){
    		var s = "{";
    		for(var i=0;i<this.keys.length;i++,s+=','){
    			var k = this.keys[i];
    			s += k+"="+this.data[k];
    		}
    		s+="}";
    		return s;
    	};
    }
    
    
    function testMap(){
    	var m = new Map();
    	m.put('key1','Comtop');
    	m.put('key2','南方电网');
    	m.put('key3','景新花园');
    	alert("init:"+m);
    	
    	m.put('key1','康拓普');
    	alert("set key1:"+m);
    	
    	m.remove("key2");
    	alert("remove key2: "+m);
    	
    	var s ="";
    	m.each(function(key,value,index){
    		s += index+":"+ key+"="+value+"
    ";
    	});
    	alert(s);
    }
    

      //testMap.htm

    <html>
    <head>
    <title>Test Map</title>
    <script language="javascript" src="map.js">
    </script>
    </head>
    <body>
    <input type="button" value="test" onclick="testMap()">
    </body>
    </html>
    

      

  • 相关阅读:
    [译]async/await中使用阻塞式代码导致死锁
    C# 对象与JSON字符串互相转换的三种方式
    C# form 传参数的几个方法
    C# 跨线程调用控件的4中方法
    Task的取消
    Task总结
    一个开发10年程序员论:学习Python最正确的步骤
    阿里大神总结的Python基础知识实例,超详细
    十条Python面试题陷阱,看看你是否会中招
    Python程序员编程10大原则,请牢牢记住,避免吃亏
  • 原文地址:https://www.cnblogs.com/yqskj/p/3349188.html
Copyright © 2020-2023  润新知