js模擬hashtable的簡單實(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)); //腳本錯誤
//return;
}
}
//移除
this.Remove = function(key) {
if (this.Contains(key)) {
delete this.ObjArr[key];
this.Count--;
}
}
//清空
this.Clear = function() {
this.ObjArr = {}; this.Count = 0;
}
}
測試代碼:
//員工
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, "這一個key已經(jīng)存在!"); //Add無效
//ht.Clear(); //清空
alert(ht.Count);
}
- js中哈希表的幾種用法總結(jié)
- javascript 哈希表(hashtable)的簡單實(shí)現(xiàn)
- JavaScript中實(shí)現(xiàn)鍵值對應(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事件循環(huán)及宏任務(wù)微任務(wù)原理解析
這篇文章主要介紹了JavaScript事件循環(huán)及宏任務(wù)微任務(wù)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
javascript開發(fā)隨筆二 動態(tài)加載js和文件
js無非就是script標(biāo)簽引入頁面,但當(dāng)項(xiàng)目越來越大的時(shí)候,單頁面引入N個js顯然不行,合并為單個文件減少了請求數(shù),但請求的文件體積卻很大2011-11-11
javascript table美化鼠標(biāo)滑動單元格變色
效果非常不錯的table美化,當(dāng)鼠標(biāo)滑過去,背景色變換效果2008-06-06
JavaScript中數(shù)組的22種方法必學(xué)(推薦)
這篇文章主要介紹了JavaScript中數(shù)組的22種方法必學(xué)(推薦)的相關(guān)資料,需要的朋友可以參考下2016-07-07
JavaScript運(yùn)動框架 多物體任意值運(yùn)動(三)
這篇文章主要為大家詳細(xì)介紹了JavaScript運(yùn)動框架的第三部分,多物體任意值運(yùn)動,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
js調(diào)用瀏覽器打印模塊實(shí)現(xiàn)點(diǎn)擊按鈕觸發(fā)自定義函數(shù)
把瀏覽器打印的功能保留并賦予到自己添加的按鈕當(dāng)中,可以在點(diǎn)擊按鈕的同時(shí)觸發(fā)自定義的函數(shù)2014-03-03

