自己用JS写的Map(用来代替Activex Scripting.Dictionary)

由于目前项目需要,要将部分代码能在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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值