Javascript实现简单的Map



最近做个项目需要大量地使用到js脚本于是花了点时间阅读了prototype及extjs等流行js库些实现js作为已经应用了多年面向过程客户端脚本语言近两年随着Ajax技术应用而再度流行起来并且在代码实现复杂度提高了许多因此需要通过模拟服务端语言些面向对象特征使得js代码编写更有抽象性、灵活性及可维护性
  Map数据结果对于服务端语言来说是再平常不过但对于js语言却没有默认实现因此我自己尝试写了个简单Map实现尽量模拟了些面向对象特征
  代码如下实现:
  
js代码ih-map.js
/**//*
*ih-map
*Copyright(c)2006-2007,Islandhill
*[email protected]
*/

//
varIh=...{};
Ih.Map=Class.create;

//stheprototypeoftheMap.
Ih.Map.prototype=...{
initialize:function...{
this.m=Array;
},
put:function(k,v)...{
varEntry=Ih.MapEntry(k,v);
for(vari=0;i<this.m.length;i)...{
varentry=this.m[i];
(entry.keyEquals(k))...{
;
}
}
this.m.push(Entry);
},
get:function(k)...{
for(vari=0;i<this.m.length;i)...{
varentry=this.m[i];
(entry.keyEquals(k))...{
entry.value;
}
}
null;
},
remove:function(k)...{
varentryPoped;
for(vari=0;i<this.m.length;i)...{
entryPoped=this.m.pop;
(entryPoped.keyEquals(k))...{ [Page]
;
}...{
this.m.unsht(entryPoped);
}
}
}
,
getSize:function...{
this.m.length;
},
getKeys:function...{
varkeys=Array;
for(vari=0;i<this.m.length;i)...{
keys.push(this.m[i].key);
}
keys;
},
getValues:function...{
varvalues=Array;
for(vari=0;i<this.m.length;i)...{
values.push(this.m[i].value);
}
values;
},
isEmpty:function...{
(this.mnull||this.m.length<=0);
},
containsKey:function(k)...{
for(vari=0;i<this.m.length;i)...{
(this.m[i].keyEquals(k))


true;
}
false;
},
putAll:function(map)...{
(mapnull||typeofmap!=\"object\")...{
throwError(\"theobjecttobeputshouldbeavalidobject\"); [Page]
}
for(vari=0;i<map.getSize;i)...{
this.put(map.m[i].key,map.m[i].value);
}
}
};


//singleentrystructureheMap
Ih.MapEntry=function(k,v)...{
this.key=k;
this.value=v;
this.keyEquals=function(key2)...{
(this.keykey2)...{
true;
}...{
false;
}
}
}



  测试代码:simpleMapTest.html 另外需包含prototype
<!DOCTYPEHTMLPUBLIC\"-//W3C//DTDHTML4.01Transitional//EN\">
<html>
<head>
<title>simpleMapTest.html</title>

<metahttp-equiv=\"keywords\"content=\"keyword1,keyword2,keyword3\">
<metahttp-equiv=\"description\"content=\"thisismypage\">
<metahttp-equiv=\"content-type\"content=\"text/html;char=UTF-8\">

<!--<linkrel=\"stylesheet\"type=\"text/css\"href=\"./styles.css\">-->

<scripttype=\"text/javascript\"src=\"js/prototype.js\"></script>
<scripttype=\"text/javascript\"src=\"js/ih-map.js\"></script>

<scripttype=\"text/javascript\">...
varmap=Ih.Map;
map.put(’name’,’islandhill’);
map.put(’gender’,’male’);
map.put(’location’,’xiamen’);

//testgetsize
alert(map.getSize);

//testget [Page]
alert(map.get(’name’)+\",\"+map.get(’gender’)+\",\"+map.get(’location’)+\",\"+map.get(’notDefined’));

//testremove,containsKey,isEmpty
map.remove(’location’);
map.remove(’notDefined’);
alert(map.containsKey(’location’));
alert(map.isEmpty);
alert(map.getSize);

//testgetKeys,getvaluesisthesamestuff
varkeys=map.getKeys;
varkeysStr=\"\";
for(vari=0;i<keys.length;i)...{

keysStrkeys[i]+\"\";
}
alert(keysStr);

//getputall
varmap2=Ih.Map;
map2.put(’dept’,’sdev’);
map2.put(’position’,’CTO’);
map.putAll(map2);
alert(map.getSize);
alert(map.get(’position’));

//test
</script>

</head>

<body>

</body>
</html>
Tags: 

延伸阅读

最新评论

发表评论