js模擬hashtable的簡(jiǎn)單實(shí)例
function Hashtable()//自定義hashtable
{
this._hash = new Object();
this.add = function(key, value) {
if (typeof (key) != "undefined") {
if (this.contains(key) == false) {
this._hash[key] = typeof (value) == "undefined" ? null : 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) { return this._hash[key]; }
this.contains = function(key) { return typeof (this._hash[key]) != "undefined"; }
this.clear = function() { for (var k in this._hash) { delete this._hash[k]; } }
}
// js哈希表
function HashTable() {
this.ObjArr = {};
this.Count = 0;
//添加
this.Add = function(key, value) {
if (this.ObjArr.hasOwnProperty(key)) {
return false; //如果鍵已經(jīng)存在,不添加
}
else {
this.ObjArr[key] = value;
this.Count++;
return true;
}
}
//是否包含某項(xiàng)
this.Contains = function(key) {
return this.ObjArr.hasOwnProperty(key);
}
//取某一項(xiàng) 其實(shí)等價(jià)于this.ObjArr[key]
this.GetValue = function(key) {
if (this.Contains(key)) {
return this.ObjArr[key];
}
else {
throw Error("Hashtable not cotains the key: " + String(key)); //腳本錯(cuò)誤
//return;
}
}
//移除
this.Remove = function(key) {
if (this.Contains(key)) {
delete this.ObjArr[key];
this.Count--;
}
}
//清空
this.Clear = function() {
this.ObjArr = {}; this.Count = 0;
}
}
測(cè)試代碼:
//員工
function employee(id, userName) {
this.id = id;
this.userName = userName;
}
function test() {
var ht = new HashTable();
var tmpEmployee = null;
for (var i = 1; i < 6; i++) {
tmpEmployee = new employee(i, "Employee_" + i);
ht.Add(i, tmpEmployee);
}
for (var i = 1; i <= ht.Count; i++) {
alert(ht.GetValue(i).userName); //其實(shí)等價(jià)于ht.ObjArr[i].userName
//alert(ht.ObjArr[i].userName);
}
ht.Remove(1);
alert(ht.Contains(1)); //false
alert(ht.Contains(2)); //true
//alert(ht.GetValue(1)); //異常
var result = ht.GetValue(2);
if (result != null) {
alert("Employee Id:" + result.id + ";UserName:" + result.userName);
}
ht.Add(2, "這一個(gè)key已經(jīng)存在!"); //Add無(wú)效
//ht.Clear(); //清空
alert(ht.Count);
}
- js中哈希表的幾種用法總結(jié)
- javascript 哈希表(hashtable)的簡(jiǎn)單實(shí)現(xiàn)
- JavaScript中實(shí)現(xiàn)鍵值對(duì)應(yīng)的字典與哈希表結(jié)構(gòu)的示例
- js實(shí)現(xiàn)HashTable(哈希表)的實(shí)例分析
- javascript實(shí)現(xiàn)獲取字符串hash值
- 淺談js多維數(shù)組和hash數(shù)組定義和使用
- javascript hashtable實(shí)現(xiàn)代碼
- js數(shù)組去重的hash方法
- js實(shí)現(xiàn)hashtable的賦值、取值、遍歷操作實(shí)例詳解
- JS模擬實(shí)現(xiàn)哈希表及應(yīng)用詳解
相關(guān)文章
JavaScript+Canvas實(shí)現(xiàn)帶跳動(dòng)效果的粒子動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)JavaScript和Canvas實(shí)現(xiàn)帶跳動(dòng)效果的粒子動(dòng)畫(huà),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2023-03-03JS獲得一個(gè)對(duì)象的所有屬性和方法實(shí)例
下面小編就為大家?guī)?lái)一篇JS獲得一個(gè)對(duì)象的所有屬性和方法實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02js點(diǎn)擊列表文字對(duì)應(yīng)該行顯示背景顏色的實(shí)現(xiàn)代碼
這篇文章主要介紹了js點(diǎn)擊列表文字對(duì)應(yīng)該行顯示背景顏色的實(shí)現(xiàn)代碼,感興趣的小伙伴可以參考下2015-08-08JavaScript下的時(shí)間格式處理函數(shù)Date.prototype.format
這篇文章主要介紹了JavaScript下的時(shí)間格式處理函數(shù)Date.prototype.format的相關(guān)資料,需要的朋友可以參考下2016-01-01javascript實(shí)現(xiàn)表格增刪改操作實(shí)例詳解
這篇文章主要介紹了javascript實(shí)現(xiàn)表格增刪改操作的實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了javascript操作表格的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05ajax分頁(yè)效果(bootstrap模態(tài)框)
這篇文章主要為大家詳細(xì)介紹了ajax分頁(yè)效果的實(shí)現(xiàn)方法,結(jié)合bootstrap模態(tài)框使用 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01js動(dòng)態(tài)添加事件并可傳參數(shù)示例代碼
js動(dòng)態(tài)添加事件可以搜索到很多的相關(guān)文章,不過(guò)可以傳參數(shù)的就沒(méi)有幾個(gè)了,下面有個(gè)不錯(cuò)的示例可以滿足大家對(duì)傳參的需求,感興趣的各位不要錯(cuò)過(guò)2013-10-10