首页 > 代码库 > javascript中模拟实现java语言中的map

javascript中模拟实现java语言中的map

//javascript中模拟实现java语言中的map

实现代码:

<!DOCTYPE html>

<html lang="en">

<head>

         <meta charset="UTF-8">

         <script type="text/javascript">

                   //private

                   var obj={};//空的对象容器,用于承装键值对

                   function Map()

                   {

                            //put方法

                            this.put=function(key,value)

                            {

                                     obj[key]=value;//把键值对绑定到js对象上

                            }

                            //用户用去map容器的个数

                            this.size=function()

                            {

                                     //定义一个变量用于记录个数

                                     var count=0;

                                     for(var attr in obj)

                                     {

                                               count++;

                                     }

                                     return count;

                            }

                            //根据key得到value值

                            this.get=function(key)

                            {

                                     /**

                                     因为在js中如果变量为0,在判断的条件中表示为false(类型自动转换)

                                     */

                                     if(obj[key]||obj[key]===0||obj[key]===false)

                                     {

                                               return obj[key];

                                     }

                                     else

                                     {

                                               return null;

                                     }

                            }

                            //根据key删除

                            this.remove=function(key)

                            {

                                     if(obj[key]||obj[key]===0||obj[key]===false)

                                     {

                                               delete obj[key];

                                     }

                            }

                            //遍历map中的元素

                            this.eachMap=function(fun)

                            {

                                     for(var attr in obj)

                                     {

                                               fun(attr,obj[attr]);

                                     }

                            }

                   }

                   //模拟java中的map

                   var m=new Map();

                   m.put("1",‘徐守威‘);

                   m.put("2",25);

                   m.put("3",true);

                   m.put("4",new Date());

                   //console.log(m.size());

                   //console.log(m.get("1"));

                   //m.remove("1");

                   //console.log(m.get("1"));

                   m.eachMap(function(key,value){

                            console.log(key+" : "+value)

                   });

         </script>

</head>

<body>

        

</body>

</html>

javascript中模拟实现java语言中的map