码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-04-12 18:35:26      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:set   his   语言   js对象   map容器   bsp   var   har   count   

//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

标签:set   his   语言   js对象   map容器   bsp   var   har   count   

原文地址:http://www.cnblogs.com/Jasxu/p/javascript_to_map.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!