Home  >  Article  >  Web Front-end  >  A simple example of js simulating hashtable_javascript skills

A simple example of js simulating hashtable_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:56:461005browse

复制代码 代码如下:

function Hashtable()//自定义hashtable
{
    this._hash = new Object();
    this.add = function(key, value) {
        if (typeof (key) != "undefined") {
            if (this.contains(key) == false) {
                this._hash[key] = typeof (value) == "undefined" ? null : value;
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    this.remove = function(key) { delete this._hash[key]; }
    this.count = function() { var i = 0; for (var k in this._hash) { i ; } return i; }
    this.items = function(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]; } }
}

复制代码 代码如下:

//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 it
}
else {
this.ObjArr[key] = value;
this.Count ;
return true;
}
}

//Whether it contains an item
this.Contains = function(key) {
return this.ObjArr.hasOwnProperty(key);
}

//Getting an item is equivalent to this.ObjArr[key]
this.GetValue = function(key) {
if (this.Contains(key)) {
return this. ObjArr[key];
                                                                                                                                                                                                                                                                 🎜> }

//Remove
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;

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);

}


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