This article is mainly on the JS simulation Hashtable simple example of the introduction, the need for friends can come to the reference, I hope to help you.
Code as follows: function Hashtable ()//custom Hashtable { This._hash = new Object () This.add = Functi On (key, value) { if (typeof (key)!= "undefined") { - if (this.contains (key) = = False) { This._hash[key] = typeof (Value ) = = "undefined"? Null:value; return true; } else { return false; & nbsp { } else { return False } } This.remove = function (key) {delete This._hash[key]; This.count = function () {var i = 0; for (var K-This._hash) {i++;} return i;} This.items = Fun Ction (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];}}} Code as follows://JS hash table function HashTable () { this. Objarr = {}; this. Count = 0; //Add this. ADD = function (key, value) { if (this. Objarr.hasownproperty (key) { return false;///If key already exists, do not add & nbsp } else { this. Objarr[key] = value; this. count++; return true; } } //Does it contain a this. Contains = function (key) { return this. Objarr.hasownproperty (key); } &nbsP //Taking an item is actually equivalent to this. Objarr[key] this. GetValue = function (key) { if (this. Contains (Key)) { return this. Objarr[key]; } else { throw Error (Ha Shtable not cotains the key: "+ String (key)"; Script error //return; } } //removal this. Remove = function (key) { if (this. Contains (Key)) { Delete this. Objarr[key]; this. count--; } } //clear this. Clear = function () { this. Objarr = {}; This. Count = 0; } Test code://Employee function employee (ID, userName) { this.id = ID;   This.username = UserName; } function Test () { var ht = new HashTable (); var tmpemployee = null; &NB Sp 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); is actually equivalent to 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)); Exception var result = ht. GetValue (2); if (result!= null) { alert ("Employee Id: + result.id +"; UserName: "+ result.username); } HT. ADD (2, "This key already exists!"); Add invalid //HT. Clear (); Empty Alert (HT. Count); }