用Javascript写的一个映射表类
2006-10-12 09:38
176 查看
用Javascript写的一个映射表类
该类可以通过关键字(key)查找相对应的值(value),关键字的类型可以是String、Number、Boolean类型,值的类型不限,代码如下:
<script>
function struct(key, value){
this.key = key;
this.value = value;
}
function setAt(key, value){
for (var i = 0; i < this.map.length; i++)
{
if ( this.map[i].key === key )
{
this.map[i].value = value;
return;
}
}
this.map[this.map.length] = new struct(key, value);
}
function lookUp(key)
{
for (var i = 0; i < this.map.length; i++)
{
if ( this.map[i].key === key )
{
return this.map[i].value;
}
}
return null;
}
function removeKey(key)
{
var v;
for (var i = 0; i < this.map.length; i++)
{
v = this.map.pop();
if ( v.key === key )
continue;
this.map.unshift(v);
}
}
function getCount(){
return this.map.length;
}
function isEmpty(){
return this.map.length <= 0;
}
function classMap() {
this.map = new Array();
this.lookUp = lookUp;
this.setAt = setAt;
this.removeKey = removeKey;
this.getCount = getCount;
this.isEmpty = isEmpty;
}
window.onload = function(){
var map = new classMap();
alert("is the map empty? " + map.isEmpty());
// string to array
map.setAt("sw1", new Array("sw1_1"));
map.setAt("sw2", new Array("sw2_1", "sw2_2"));
map.setAt("sw3", new Array("sw3_1", "sw3_2", "sw3_3"));
alert(map.lookUp("sw5")); // null
alert(map.lookUp("sw2")); // "sw2_1, sw2_2"
alert(map.getCount()); // 3
// number to string
map.setAt(1, "sw1");
map.setAt(2, "sw2");
alert(map.lookUp(2)); // "sw2"
map.setAt(2, new Array("sw2_1", "sw2_2"));
alert(map.lookUp(2)); // "sw2_1, sw2_2"
alert(map.getCount()); // 5
// string to number
map.setAt("1", 1);
map.setAt("2", 2);
alert(map.lookUp("1")); // 1
alert(map.lookUp(1)); // "sw1"
map.setAt("sw3", 33);
alert(map.lookUp("sw3")); // 33
alert(map.getCount()); // 7
// number to number
map.setAt(1, 11);
map.setAt(2, 22);
alert(map.lookUp(1)); // 11
alert(map.getCount()); // 7
map.removeKey(1);
alert(map.lookUp(1)); // null
alert(map.getCount()); // 6
// boolean to array
map.setAt(false, new Array("false", "true"));
alert(map.lookUp(false));
alert(map.getCount()); // 7
}
</script>
该类可以通过关键字(key)查找相对应的值(value),关键字的类型可以是String、Number、Boolean类型,值的类型不限,代码如下:
<script>
function struct(key, value){
this.key = key;
this.value = value;
}
function setAt(key, value){
for (var i = 0; i < this.map.length; i++)
{
if ( this.map[i].key === key )
{
this.map[i].value = value;
return;
}
}
this.map[this.map.length] = new struct(key, value);
}
function lookUp(key)
{
for (var i = 0; i < this.map.length; i++)
{
if ( this.map[i].key === key )
{
return this.map[i].value;
}
}
return null;
}
function removeKey(key)
{
var v;
for (var i = 0; i < this.map.length; i++)
{
v = this.map.pop();
if ( v.key === key )
continue;
this.map.unshift(v);
}
}
function getCount(){
return this.map.length;
}
function isEmpty(){
return this.map.length <= 0;
}
function classMap() {
this.map = new Array();
this.lookUp = lookUp;
this.setAt = setAt;
this.removeKey = removeKey;
this.getCount = getCount;
this.isEmpty = isEmpty;
}
window.onload = function(){
var map = new classMap();
alert("is the map empty? " + map.isEmpty());
// string to array
map.setAt("sw1", new Array("sw1_1"));
map.setAt("sw2", new Array("sw2_1", "sw2_2"));
map.setAt("sw3", new Array("sw3_1", "sw3_2", "sw3_3"));
alert(map.lookUp("sw5")); // null
alert(map.lookUp("sw2")); // "sw2_1, sw2_2"
alert(map.getCount()); // 3
// number to string
map.setAt(1, "sw1");
map.setAt(2, "sw2");
alert(map.lookUp(2)); // "sw2"
map.setAt(2, new Array("sw2_1", "sw2_2"));
alert(map.lookUp(2)); // "sw2_1, sw2_2"
alert(map.getCount()); // 5
// string to number
map.setAt("1", 1);
map.setAt("2", 2);
alert(map.lookUp("1")); // 1
alert(map.lookUp(1)); // "sw1"
map.setAt("sw3", 33);
alert(map.lookUp("sw3")); // 33
alert(map.getCount()); // 7
// number to number
map.setAt(1, 11);
map.setAt(2, 22);
alert(map.lookUp(1)); // 11
alert(map.getCount()); // 7
map.removeKey(1);
alert(map.lookUp(1)); // null
alert(map.getCount()); // 6
// boolean to array
map.setAt(false, new Array("false", "true"));
alert(map.lookUp(false));
alert(map.getCount()); // 7
}
</script>
相关文章推荐
- 用Javascript写的一个映射表类
- 用Javascript写的一个映射表类
- 用Javascript写的一个映射表(MAP)类--续
- 用Javascript写的一个映射表类
- JavaScript 如何弹出一个对话框窗口
- Javascript:一个屌丝的逆袭
- Javascript打印一个对象中所有属性和方法
- Javascript-Mozilla和IE中的一个函数直接量的问题
- struts2 中映射文件 一个列在resultMap中引用2次必须都列出来
- 在JavaScript中如何判断一个字符串中是否包含某个字符串
- javascript同一个事件如何注册多个处理函数
- Web: 使用Javascript新开一个浏览器标签而不是新开一个窗口
- 介绍一个Open Source的Javascript UI Lib
- 一个必用的javascript框架:underscore.js
- arcgis api for javascript 4.0 入门学习(1)创建一个2D地图
- javascript与jQuery设置取得div绝对位置一个小应用(像日历控件一样,在编辑框下面显示一个层)
- 一个程序明白Javascript绘图
- 一个ADF Javascript 添加鼠标移动事件的例子
- [javascript] Box2D JS初探(一个控制小球的游戏例子)。学习用!
- Hibernate Composite-element映射的一个简单例子