日期:2014-05-16  浏览次数:20479 次

JavaScript实现Java的Map、List功能(其他论坛上看到的)

JavaScript实现Java的Map、List功能,如下代码

function?HashMap(){?

????this.size=0;?

????this.map=new?Object();?

}?

?

HashMap.prototype.put=function(key,value){?

????if(!this.map[key]){?

????????this.size++;?

????}?

????this.map[key]=value;?

};?

HashMap.prototype.get=function(key){?

????return?this.isKey(key)?this.map[key]:null;?

};?

HashMap.prototype.isKey=function(key){?

????return?(key?in?this.map);?

};?

HashMap.prototype.remove=function(key){?

??if(?this.isKey(key)?&&?(delete?this.map[key])){???

????????this.size--;???

??}???

};?

?

HashMap.prototype.size=function(){?

????return?this.size;?

};?

?

HashMap.prototype.find=function(_callback){?

????for(var?_key?in?this.map){?

????????_callback.call(this,_key,this.map[_key]);?

????}?

};?

//List?

?

function?ArrayList(){?

????this.size=0;?

????this.list=new?Object();?

};?

ArrayList.prototype.add=function(obj){?

????this.list[this.size++];?

????return?this.size;?

};?

ArrayList.prototype.remove=function(index