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

Posted 徐守威

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

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

好程序员前端学习路线分享模拟JavaScript中面向对象技术

JavaScript中的继承实现

js JQUERY

JavaScript使用接口(转载)

华为OD机试真题 JavaScript 实现模拟商场优惠打折II2023 Q1 | 100分