首页 > 代码库 > javascript模仿map

javascript模仿map

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="http://www.mamicode.com/">
    
    <title>My JSP ‘test3.jsp‘ starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">
    -->
    <script src="http://www.mamicode.com/js/jquery-2.1.1.min.js"></script>
    <script>
        //模拟map
        function Map(){
            var object={};    //空的对象,用于承载键值对
            this.put=function(key,value){
                object[key]=value;      //把键值对绑定到object上
            }
            this.size=function(){
                var count=0;
                for(var attr in object){
                    count++;    
                }
                return count;
            }
            this.get=function(key){
                if(object[key]||object[key]===0||object[key]===false){
                    return object[key];
                }else{
                    return null;
                }
            }
            this.remove=function(key){
                if(object[key]||object[key]===0||object[key]===false){
                    delete object[key];
                }
            }
            this.eachmap=function(fn){
                for(var attr in object){
                    fn(attr,object[attr]);
                }
            }
        }
        var map=new Map();
        map.put(‘1‘,‘1‘);
        map.put(‘2‘,‘2‘);
        map.put(‘3‘,‘3‘);
        alert(map.size());
        alert(‘2‘);
        map.eachmap(function(key,value){
            alert(key+":"+value);
        });
    </script>
  </head>
  
  <body>
    This is my JSP page. <br>
  </body>
</html>


本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1878720

javascript模仿map