由于目前项目需要,要将部分代码能在FF中显示,所以老代码中的通过new ActiveXObject("Scripting.Dictionary")实现的Map则不能使用,通过[url=http://zozoh.iteye.com/]zozoh[/url]的指点我自己写了一个,目前已在项目中使用,现在分享给大家。
如果哪里有错误希望大家批评。
如果哪里有错误希望大家批评。
function Map()
{
this.map = {}; //initialize with an empty array
this.Count=0;
}
Map.prototype.size = function()
{
return this.Count;
}
Map.prototype.Item = function(key)
{
if (this.Exists(key)) {
return this.map[key];
}else{
return null;
}
}
Map.prototype.Add = function(key, value){
if (!this.Exists(key)) {
this.Count++;
}
this.map[key] = value;
}
Map.prototype.Items = function()
{
var array = [];
for(var key in this.map){
array.push(this.map[key]);
}
return array;
}
Map.prototype.Keys = function()
{
var array = [];
for(var key in this.map){
array.push(key);
}
return array;
}
Map.prototype.Exists = function(key)
{
if(typeof this.map[key]!="undefined"){
return true;
}else{
return false;
}
}
Map.prototype.Remove = function(key)
{
var newMap={};
for(var i in this.map){
if(i!=key){
newMap[i]=this.map[i];
}else{
this.Count--;
}
}
this.map = newMap;
}
Map.prototype.RemoveAll = function()
{
this.map = {};
this.Count=0;
}