Home  >  Article  >  Web Front-end  >  Create a JavaScript hash table Hashtable

Create a JavaScript hash table Hashtable

高洛峰
高洛峰Original
2016-11-28 15:36:541169browse

Hashtable is one of the most commonly used data structures, but there are no various data structure objects in JavaScript. However, we can use some features of dynamic languages ​​to implement some commonly used data structures and operations, which can make some complex code logic clearer and more in line with the encapsulation principle advocated by object-oriented programming. This is actually the use of the JavaScriptObject object's feature of dynamically adding attributes to implement Hashtable. What needs to be explained here is that JavaScript can traverse all attributes in the Object through the for statement. But this method should generally be avoided unless you really know what is put in your object.

<script type="text/javascript">
function Hashtable() {  
    this._hashValue= new Object();  
    this._iCount= 0;  
}  
 
Hashtable.prototype.add = function(strKey, value) {  
    if(typeof (strKey) == "string"){  
        this._hashValue[strKey]= typeof (value) != "undefined"? value : null;  
        this._iCount++;  
         returntrue;  
    }  
    else 
        throw"hash key not allow null!";  
}  
 
Hashtable.prototype.get = function (key) {  
    if (typeof (key)== "string" && this._hashValue[key] != typeof(&#39;undefined&#39;)) {  
        returnthis._hashValue[key];  
    }  
    if(typeof (key) == "number")  
        returnthis._getCellByIndex(key);  
    else 
        throw"hash value not allow null!";  
 
    returnnull;  
}  
 
Hashtable.prototype.contain = function(key) {  
    returnthis.get(key) != null;  
}  
 
Hashtable.prototype.findKey = function(iIndex) {  
    if(typeof (iIndex) == "number")  
        returnthis._getCellByIndex(iIndex, false);  
    else 
        throw"find key parameter must be a number!";  
}  
 
Hashtable.prototype.count = function () {  
    returnthis._iCount;  
} 
  
Hashtable.prototype._getCellByIndex = function(iIndex, bIsGetValue) {  
    vari = 0;  
    if(bIsGetValue == null) bIsGetValue = true;  
    for(var key in this._hashValue) {  
        if(i == iIndex) {  
            returnbIsGetValue ? this._hashValue[key] : key;  
        }  
        i++;  
    }  
    returnnull;  
}  
 
Hashtable.prototype.remove = function(key) {  
    for(var strKey in this._hashValue) {  
        if(key == strKey) 
        {  
            deletethis._hashValue[key];  
            this._iCount--;  
        }  
    }  
}  
 
Hashtable.prototype.clear = function () {  
    for (var key in this._hashValue) {  
        delete this._hashValue[key];  
    }  
    this._iCount = 0;  
}  
</script>

StringCollection/ArrayList/Stack/Queue, etc. can all use this idea to extend JavaScript.

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