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

Posted 徐守威

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript中模拟实现java语言中的map相关的知识,希望对你有一定的参考价值。

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

<!DOCTYPE html>

<html lang="en">

<head>

         <metacharset="UTF-8">

         <scripttype="text/javascript">

                   //private

                   varobj=;//空的对象容器,用于承装键值对

                   functionMap()

                  

                            //put方法

                            this.put=function(key,value)

                           

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

                           

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

                            this.size=function()

                           

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

                                     varcount=0;

                                     for(varattr in obj)

                                    

                                               count++;

                                    

                                     returncount;

                           

                            //根据key得到value值

                            this.get=function(key)

                           

                                     /**

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

                                     */

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

                                    

                                               returnobj[key];

                                    

                                     else

                                    

                                               returnnull;

                                    

                           

                            //根据key删除

                            this.remove=function(key)

                           

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

                                    

                                               deleteobj[key];

                                    

                           

                            //遍历map中的元素

                            this.eachMap=function(fun)

                           

                                     for(varattr in obj)

                                    

                                               fun(attr,obj[attr]);

                                    

                           

                  

                   //模拟java中的map

                   varm=new Map();

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

                   m.put("2",25);

                   m.put("3",true);

                   m.put("4",newDate());

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