//哈希表
function HashTable() {
//存数据的实体
this._Hash = new Object();
//新增键值
this.Add = function (key, value) {
if (typeof (key) != "undefined") {
if (this.Contains(key) == false) {
this._Hash[key] = value;
return true;
}
else {
return false;
}
}
else {
return false;
}
}
//删除键值
this.Remove = function (key) { delete this._Hash[key]; }
//数量
this.Count = function () { var i = 0; for (var k in this._Hash) { i++; } return i; }
//获取值
this.Items = function (key) {
var value = this._Hash[key];
return typeof (value) == "undefined" ? null : value
}
//获取值
this.GetValue = function (key) {
return this.Items(key);
}
//获取值
this.Get = function (key) {
return this.Items(key);
}
//赋值
this.SetValue = function (key, value) {
return this._Hash[key] = value;
}
//是否包含键
this.Contains = function (key) { return typeof (this._Hash[key]) != "undefined"; }
//清空哈希表
this.Clear = function () { for (var k in this._Hash) { delete this._Hash[k]; } }
//获取所有键
this.AllKeys = function () { return this._Hash; }
}
新建: var ht = new HashTable();
添加: ht.Add("kk","hello");
遍历:
<pre class="prettyprint javascript" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-top: 0px; margin-bottom: 1.5em; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;"><span style="white-space:pre"> </span>for</span></span> (<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">var</span></span> i = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">1</span></span>; i < <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">6</span></span>; i++<span style="color: rgb(0, 0, 0);">) {
<span class="indent"> </span><span class="indent"> </span><span class="indent"> </span><span class="indent"> </span>tmpEmployee </span>= <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">new</span></span> employee(i, <span style="color: rgb(128, 0, 0);"><span class="string" style="color: rgb(221, 17, 68);">"</span></span><span class="string" style="color: rgb(221, 17, 68);"><span style="color: rgb(128, 0, 0);">Employee_</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span> +<span style="color: rgb(0, 0, 0);"> i);
<span class="indent"> </span><span class="indent"> </span><span class="indent"> </span><span class="indent"> </span>ht.Add(i, tmpEmployee);
<span class="indent"> </span><span class="indent"> </span><span class="indent"> </span>}
<span class="indent"> </span><span class="indent"> </span><span class="indent"> </span></span><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">for</span></span> (<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">var</span></span> i = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">1</span></span>; i <= ht.Count; i++<span style="color: rgb(0, 0, 0);">) {
<span class="indent"> </span><span class="indent"> </span><span class="indent"> </span><span class="indent"> </span>alert(ht.GetValue(i).userName); </span><span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);">其实等价于ht.ObjArr[i].userName </span></span><span style="color: rgb(0, 128, 0);">
<span class="indent"> </span><span class="indent"> </span><span class="indent"> </span><span class="indent"> </span></span><span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);">alert(ht.ObjArr[i].userName); </span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 0, 0);"><span class="indent"> </span><span class="indent"> </span><span class="indent"> </span>} </span>
for (var i = 1; i <= ht.Count; i++) {
alert(ht.GetValue(i)); //其实等价于ht.ObjArr[i].userName
}
参考:http://www.tuicool.com/articles/67NVzq