<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        
        <script type="text/javascript" charset="utf-8">
    
            function Map(){
                
                //private 不允许外部访问属性
                var  obj = {};  //空的对象容器,盛装键值对
                
                this.put = function(key , value){
                    obj[key] = value;    //把键值对象绑定到obj对象                    
                }
                //map大小
                this.size = function(){
                    var count = 0;
                    for(var attr in obj){
                        count ++;
                    }
                    return count;
                }
                
                //get 方法 根据key得到value
                this.get = function(key){
                    //处理 0、false
                    if (obj[key] || obj[key] === 0 || obj[key] == false) {
                        return obj[key];
                    }
                    else{
                        return null;
                    } 
                }
                //remove 方法
                this.remove = function(key){
                    if (obj[key] || obj[key] === 0 || obj[key] == false) {
                        delete obj[key];
                    }
                }
                //eachMap 遍历map容器的方法
                this.eachMap = function(fn){
                    for(var attr in obj){
                        fn(attr, obj[attr]);
                    }
                }
            }
            
            //模拟java的map
            var  map = new Map();
            map.put('01','abc');
            map.put('02',0);
            map.put('03',false);
            map.put('04',new Date());
            
            //alert(map.size());
            //alert(map.get('03'));
            //map.remove('03');
            //alert(map.get('03'));
            
            map.eachMap(function(key,value){
                alert(key+":" + value);
            });
            
            
        </script>
        
        
    </head>
    <body>
    </body>
</html>

 

欢迎关注个人公众号一起交流学习: