<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" charset="UTF-8"> function Map(){ var obj = {}; this.put = function(key , value){ obj[key] = value; } //size()获取map的长度 this.size = function(){ var count = 0; for(var attr in obj){ count++; } return count; } //get() 由键取值 this.get = function(key){ if(obj[key] || obj[key]===0 || obj[key] === false){ return obj[key]; }else{ return null; } } //remove() :移除相应的键值对 this.remove = function(key){ if(obj[key] || obj[key]===0 || obj[key] === false){ var val = obj[key] ; delete obj[key]; return val; } } //eachMap() : 遍历map this.eachMap = function(){ for(var attr in obj) console.log(attr+" : "+obj[attr]) } } var m = new Map(); m.put('01','a'); m.put('02',true); m.put('03',new Date()); console.log(m.size());//3 console.log(m.get('01')); m.eachMap() ; console.log(m.remove('02')); m.eachMap(); </script> </head> <body> </body> </html>
时间: 2024-10-16 16:01:36