JS simulation to achieve a similar C # hashtable simple function code _javascript skills

Source: Internet
Author: User
Tags hasownproperty
If in C #, we can easily implement this function simply by using the Hashtable or dictionary to take value from the key. In fact, we do a little processing, JS can also achieve similar hashtable functions. The following summary of the author of the development of the implementation of the way, the main code paste.
1, the implementation of ideas: the main use of the prototype (prototype) of the hasOwnProperty method to determine whether the object of the item is to add, remove or remove a matching item. hasOwnProperty is more dexterous than traversing an array: at least it is O (1) complex in terms of code.
2, implementation code
Copy Code 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 the key already exists, do not add
}
Else {
this. Objarr[key] = value;
this. count++;
return true;
}
}
//contains an item
this. Contains = function (key) {
return this. Objarr.hasownproperty (key);
}
//takes an item that is actually 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--;
}
}
//Empty
this. Clear = function () {
this. Objarr = {}; This. Count = 0;
}
}

3. Test code
Code
Copy Code code as follows:

Employees
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); 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)); Abnormal
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 not valid
Ht. Clear (); Empty
Alert (HT. Count);
}

The call is very simple, as long as a new Hashtable object, common features are available. Is it simple? Enjoy it.
Summary: The prototype chain (prototype chain) and the scope chain are the two core parts of JS. Learn and understand them, many complex problems will be solved; take advantage of their features, we can easily achieve very flexible and efficient functions.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.