时间:2021-07-01 10:21:17 帮助过:15人阅读
<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('undefined')) { 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等等都可以借鉴这个思路来对JavaScript 进行扩展。