Prototype使用学习手册指南之Hash.js
- keys(): 返回hash的键值数组
- values(): 返回值得数组
- merge(hash): 合并两个hash
- toQueryString(): 跟string的toQueryParams方法想法,将hash转化为一个querystring, 会调用encodeURIComponent对键和值进行编码
- inspect(): hash的字符串表示
因为hash只是javascript的一个普通的对象而已,所以添加一个键值对使用: hash[key]=value就可以了,删除一个键值对使用 detele hash[key]就可以了
另外还可以使用下面来实现js的hash表,嘻嘻
<script type="text/javascript">
<!--
function Hash() {
this.length = 0;
this.items = new Array();
for (var i = 0; i < arguments.length; i += 2) {
if (typeof(arguments[i + 1]) != 'undefined') {
this.items[arguments[i]] = arguments[i + 1];
this.length++;
}
}
this.removeItem = function(in_key)
{
var tmp_value;
if (typeof(this.items[in_key]) != 'undefined') {
this.length--;
var tmp_value = this.items[in_key];
delete this.items[in_key];
}
return tmp_value;
}
this.getItem = function(in_key) {
return this.items[in_key];
}
this.setItem = function(in_key, in_value)
{
if (typeof(in_value) != 'undefined') {
if (typeof(this.items[in_key]) == 'undefined') {
this.length++;
}
this.items[in_key] = in_value;
}
return in_value;
}
this.hasItem = function(in_key)
{
return typeof(this.items[in_key]) != 'undefined';
}
}
var myHash = new Hash(
'one', 1,
'two', 2,
'three', 3,
);
document.write( myHash.getItem('two') );
//-->
</script>
<form name="h0">
KEY: <input type="text" name="h1" value="two" />
VALUE: <input type="text" name="h2" value="222222" />
<input type="button" value="setItem" onclick="myHash.setItem(h0.h1.value,h0.h2.value);" />
<input type="button" value="getItem" onclick="alert(myHash.getItem(h0.h1.value));" />







