首頁  >  文章  >  web前端  >  JAVASCRIPT HashTable_javascript技巧

JAVASCRIPT HashTable_javascript技巧

WBOY
WBOY原創
2016-05-16 19:20:16974瀏覽

function 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];}}



var a = new Hashtable();

a.add("aa");
a.add("bb",2342);
a.add("bb",2342);

a.remove("aa");

alert(a.count());

alert(a.contains("bb"));

alert(a.contains("aa"));

alert(a.items("bb"));

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn