luocheng
2014-11-08 11:29:24
js面向对象定义HashMap对象
HashMap=function(){ this.keys = new Array(); this.data = new Object(); this.put = function(key,value){ if(this.keys[key]==null){ this.keys.push(key); } this.data[key] = value; }; this.get = function(key){ return this.data[key]; }; this.remove = function(key){ this.keys.remove(key); this.data[key]=null; } this.each = function(fn){ if(typeof(fn) == "function"){ return; } var len = this.keys.length; for ( var index = 0; index < len; index++) { var key = this.keys[index]; fn(key,this.data[key],index); } }; this.entrys = function(){ var len = this.keys.length; var entrys = new Object(); for ( var index = 0; index < len; index++) { entrys[index]={ key:this.keys[index], value:this.data[this.keys[index]] } } return entrys; }; this.isEmpty = function(){ return this.keys.length==0; }; this.size = function(){ return this.keys.length; }; this.toString = function(){ var s="{"; var len = this.keys.length; for ( var index = 0; index < len; index++,s+=",") { var key = this.keys[index]; s+= key+"="+this.data[key]; } s+="}"; return s; }; }
由最代码官方编辑于2014-11-10 20:29:37
猜你喜欢
请下载代码后再发表评论
相关代码
最近下载