1 function Hashtable (){
2 this. _ hashValue = new Object ();
3 this. _ iCount = 0;
4}
5 Hashtable. prototype. add = function (strKey, value ){
6 if (typeof (strKey) = "string "){
7 this. _ hashValue [strKey] = typeof (value )! = "Undefined "? Value: null;
8 this. _ iCount ++;
9 returntrue;
10}
11 else
12 throw "hash key not allow null! ";
13}
14 Hashtable. prototype. get = function (key ){
15 if (typeof (key) = "string" & this. _ hashValue [key]! = Typeof ('undefined ')){
16 returnthis. _ hashValue [key];
17}
18 if (typeof (key) = "number ")
19 returnthis. _ getCellByIndex (key );
20 else
21 throw "hash value not allow null! ";
22
23 returnnull;
24}
25 Hashtable. prototype. contain = function (key ){
26 returnthis. get (key )! = Null;
27}
28 Hashtable. prototype. findKey = function (iIndex ){
29 if (typeof (iIndex) = "number ")
30 returnthis. _ getCellByIndex (iIndex, false );
31 else
32 throw "find key parameter must be a number! ";
33}
34 Hashtable. prototype. count = function (){
35 returnthis. _ iCount;
36} www.2cto.com
37 Hashtable. prototype. _ getCellByIndex = function (iIndex, bIsGetValue ){
38 vari = 0;
39 if (bIsGetValue = null) bIsGetValue = true;
40 for (var key in this. _ hashValue ){
41 if (I = iIndex ){
42 returnbIsGetValue? This. _ hashValue [key]: key;
43}
44 I ++;
45}
46 returnnull;
47}
48 Hashtable. prototype. remove = function (key ){
49 for (var strKey in this. _ hashValue ){
50 if (key = strKey ){
51 deletethis. _ hashValue [key];
52 this. _ iCount --;
53}
54}
55}
56 Hashtable. prototype. clear = function (){
57 for (var key in this. _ hashValue ){
58 delete this. _ hashValue [key];
59}
60 this. _ iCount = 0;
61}
Explanation: Hashtable is one of the most common data structures in c #, but there are no data structure objects in JavaScript. However, we can use some features of Dynamic Language to implement some common data structures and operations, which can make some complex code logic clearer, it is also more in line with the packaging principles advocated by object-oriented programming. Here, we can use the JavaScriptObject Object to dynamically add attributes to implement Hashtable. here we need to note that JavaScript can traverse all attributes of the Object through the for statement. However, this method should be avoided unless you really know what is put in your object.
[By the way]: StringCollection/ArrayList/Stack/Queue can be used to expand JavaScript.
From the rain of November