Home  >  Article  >  Web Front-end  >  js simulates simple functional code similar to hashtable under c#_javascript skills

js simulates simple functional code similar to hashtable under c#_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:35:521073browse

If we are in C#, we can easily implement this function as long as we use hashtable or dictionary to get the value based on the key. In fact, if we deal with it a little bit, js can also implement functions similar to hashtable. Below is a summary of the implementation methods used in the author's development, focusing on pasting the code.
1. Implementation idea: The main purpose is to use the hasOwnProperty method of the prototype to determine whether the item in the object should be added, removed, or a matching item should be taken out, etc. The reason hasOwnProperty is smarter and faster than traversing array values ​​is that, at least from a code perspective, it has O(1) complexity.
2. Implementation code

Copy 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);
}
//Getting a certain item is actually equivalent to this.ObjArr[key]
this.GetValue = function(key) {
if (this.Contains(key) ) {
return this.ObjArr[key];
}
else {
throw Error("Hashtable not contains 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
Copy code The code is as follows:

//Employee
function(employeeid, 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);
}

It is very simple to call. As long as you create a new hashtable object, common functions will be available. Isn't it very simple? Enjoy it.
Summary: The prototype chain (prototype chain) and the scope chain are the two core parts of js. By learning and understanding them, many complex problems will be easily solved; by making good use of their characteristics, we can easily implement very flexible and efficient functions.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn