luocheng的gravatar头像
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


打赏

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友