Javascript is increasingly not as powerful as c # in processing collections. For example, in actual development, it is often used to retrieve an item that meets certain conditions in a one-dimensional array or two-dimensional array. The common processing method is to traverse the array, compare the conditions, and retrieve the matching, then end the loop. In c #, we can easily implement this function by using hashtable or dictionary to obtain value based on the key feature. As a matter of fact, js can also implement features similar to hashtable. Below is a summary of the implementation methods used in the development of the author, mainly by code.
1. Implementation idea: The main idea is to use the hasOwnProperty method of prototype to determine whether an item in the object is to be added, removed, or retrieved. HasOwnProperty is less efficient than traversing arrays because it is O (1) complex, at least in code.
2. Implementation Code
The Code is 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 the key already exists, do not add
}
Else {
This. ObjArr [key] = value;
This. Count ++;
Return true;
}
}
// Whether it contains an item
This. Contains = function (key ){
Return this. ObjArr. hasOwnProperty (key );
}
// Retrieving an item is equivalent to 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); // Script Error
// Return;
}
}
// Remove
This. Remove = function (key ){
If (this. Contains (key )){
Delete this. ObjArr [key];
This. Count --;
}
}
// Clear
This. Clear = function (){
This. ObjArr ={}; this. Count = 0;
}
}
3. Test code
Code
The Code is as follows:
// Employee
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); // 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 is invalid
// Ht. Clear (); // Clear
Alert (ht. Count );
}
Calling is simple. as long as a new hashtable object is called, common functions are available. Is it easy? Enjoy it.
Summary: prototype chain and scope chain are the two core parts of js. Learn and understand them, and solve many complex problems. By taking advantage of their features, we can easily implement very flexible and efficient functions.