透過Javascript的Array實作出來的類Hashtable,把一些物件導向的概念帶進來,應用上也還算方便:
以下是透過Javascript的Array實作出來的類Hashtable,把一些物件導向的概念帶進來,應用上也還算方便:
001
function Hashtable(){
002
this.Clear = Hash_Clear;
003
this.ContainsKey = Hash_ContainsKey;
004
this.ContainsValue = Hash_ContainsValue;
005
this.Items = Hash_Items;
006
this.IEmpty = Hash_IsEmpty;
007
this.Keys = Hash_Keys;
008
this.Put = Hash_Put;
009
this.Remove = Hash_Remove;
010
this.Size = Hash_Size;
011
this.ToString = Hash_ToString;
012
this.Values = Hash_Values;
013
this.hashtable = new Array();
014
}
015
016
//清除所有選項
017
function Hash_Clear(){
018
this.hashtable = new Array();
019
}
020
021
//判斷某個key是否存在
022
function Hash_ContainsKey(key){
023
var exists = false;
024
for (var i in this.hashtable) {
025
if (i == key && this.hashtable[i] != null) {
026
exists = true;
027
break;
028
}
029
}
030
return exists;
031
}
032
033
//判斷某個value是否存在
034
function Hash_ContainsValue(value){
035
var contains = false;
036
if (value != null) {
037
for (var i in this.hashtable) {
038
if (this.hashtable[i] == value) {
039
contains = true;
040
break;
041
}
042
}
043
}
044
return contains;
045
}
046
047
//取得某個item
048
function Hash_Items(key){
049
return this.hashtable[key];
050
}
051
052
//判斷hash是否為空
053
function Hash_IsEmpty(){
054
return (parseInt(this.size()) == 0) ? true : false;
055
}
056
057
//取得所有的key集合
058
function Hash_Keys(){
059
var keys = new Array();
060
for (var i in this.hashtable) {
061
if (this.hashtable[i] != null)
062
keys.push(i);
063
}
064
return keys;
065
}
066
067
//取得所有的value集合
068
function Hash_Values(){
069
var values = new Array();
070
for (var i in this.hashtable) {
071
if (this.hashtable[i] != null)
072
values.push(this.hashtable[i]);
073
}
074
return values;
075
}
076
077
//加入新的item
078
function Hash_Put(key, value){
079
if (key == null || value == null) {
080
throw "NullPointerException {"" + key + "},{" + value + "};
081
}else{
082
this.hashtable[key] = value;
083
}
084
}
085
086
//移除某個item
087
function Hash_Remove(key){
088
var rtn = this.hashtable[key];
089
this.hashtable[key] = null;
090
return rtn;
091
}
092
093
//取得目前hash的大小
094
function Hash_Size(){
095
var size = 0;
096
for (var i in this.hashtable) {
097
if (this.hashtable[i] != null)
098
size ++;
099
}
100
return size;
101
}
102
103
//轉成{key},{value}的字串型態
104
function Hash_ToString(){
105
var result = "";
106
for (var i in this.hashtable)
107
{
108
if (this.hashtable[i] != null)
109
result += "{"\n" + i + "},{" + this.hashtable[i] + "};
110
}
111
return result;
112
}
function Hashtable(){ 002
this.Clear = Hash_Clear; 003
this.ContainsKey = Hash_ContainsKey; 004
this.ContainsValue = Hash_ContainsValue; 005
this.Items = Hash_Items; 006
this.IEmpty = Hash_IsEmpty; 007
this.Keys = Hash_Keys; 008
this.Put = Hash_Put; 009
this.Remove = Hash_Remove; 010
this.Size = Hash_Size; 011
this.ToString = Hash_ToString; 012
this.Values = Hash_Values; 013
this.hashtable = new Array(); 014
} 015
016
//清除所有選項 017
function Hash_Clear(){ 018
this.hashtable = new Array(); 019
} 020
021
//判斷某個key是否存在 022
function Hash_ContainsKey(key){ 023
var exists = false; 024
for (var i in this.hashtable) { 025
if (i == key && this.hashtable[i] != null) { 026
exists = true; 027
break; 028
} 029
} 030
return exists; 031
} 032
033
//判斷某個value是否存在 034
function Hash_ContainsValue(value){ 035
var contains = false; 036
if (value != null) { 037
for (var i in this.hashtable) { 038
if (this.hashtable[i] == value) { 039
contains = true; 040
break; 041
} 042
} 043
} 044
return contains; 045
} 046
047
//取得某個item 048
function Hash_Items(key){ 049
return this.hashtable[key]; 050
} 051
052
//判斷hash是否為空 053
function Hash_IsEmpty(){ 054
return (parseInt(this.size()) == 0) ? true : false; 055
} 056
057
//取得所有的key集合 058
function Hash_Keys(){ 059
var keys = new Array(); 060
for (var i in this.hashtable) { 061
if (this.hashtable[i] != null) 062
keys.push(i); 063
} 064
return keys; 065
} 066
067
//取得所有的value集合 068
function Hash_Values(){ 069
var values = new Array(); 070
for (var i in this.hashtable) { 071
if (this.hashtable[i] != null) 072
values.push(this.hashtable[i]); 073
} 074
return values; 075
} 076
077
//加入新的item 078
function Hash_Put(key, value){ 079
if (key == null || value == null) { 080
throw "NullPointerException {"" + key + "},{" + value + "}; 081
}else{ 082
this.hashtable[key] = value; 083
} 084
} 085
086
//移除某個item 087
function Hash_Remove(key){ 088
var rtn = this.hashtable[key]; 089
this.hashtable[key] = null; 090
return rtn; 091
} 092
093
//取得目前hash的大小 094
function Hash_Size(){ 095
var size = 0; 096
for (var i in this.hashtable) { 097
if (this.hashtable[i] != null) 098
size ++; 099
} 100
return size; 101
} 102
103
//轉成{key},{value}的字串型態 104
function Hash_ToString(){ 105
var result = ""; 106
for (var i in this.hashtable) 107
{ 108
if (this.hashtable[i] != null) 109
result += "{"\n" + i + "},{" + this.hashtable[i] + "}; 110
} 111
return result; 112
}如果會寫物件導向的話,它的用法也很物件導向,舉個例子:
1
var tHashtable = new Hashtable();
2
tHashtable.Put('A', 'B');
3
alert(tHashtable.Items('A'));
新增一個Hashtable物件,然後呼叫Put方法將A加入,其值為B,接著透過Items方法取得A的值,這就是一個簡單的用法,但Javascript並沒有什麼封裝的概念,所以你直接呼叫Hash_Put也可以達到相同的目的。
之前在寫這個時發現一件有趣的事情,那就是Javascript的Array的key原來也可以是其他文字,以前都以為它只能是數字,例如array[0] = 'B';,後來發現array['A'] = 'B';也是可以的,算是個有趣的小發現吧。
![]() |
游舒帆 (gipi) 探索原力Co-founder,曾任TutorABC協理與鼎新電腦總監,並曾獲選兩屆微軟最有價值專家 ( MVP ),離開職場後創辦探索原力,致力於協助青少年培養面對未來的能力。認為教育與組織育才其實息息相關,都是在為未來儲備能量,2018年起成立為期一年的專題課程《職涯躍升的關鍵24堂課》,為培養台灣未來的領袖而努力。 |
function Hash_Clear()
function Hash_ContainsKey(key)
