javascript模拟map
<!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>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。