var MapClass =
... {
map : new Array(),
//Update or Insert
setAt : function(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);
},
//Query
lookUp : function(key)
...{
for (var i = 0; i < this.map.length; i++)
...{
if ( this.map[i].key === key )
...{
return this.map[i].value;
}
}
return null;
},
//Delete
removeKey : function(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);
}
},
getCount: function()...{
return this.map.length;
},
isEmpty : function()...{
return this.map.length <= 0;
}
} ;
function struct(key, value) ... {
this.key = key;
this.value = value;
}
上述代码中,struct类定义为常规类定义方式,MapClass为非常规定义方式,可实现页面内的单例模式。