js類比hashtable的簡單一實例介紹

來源:互聯網
上載者:User

 本篇文章主要是對js類比hashtable的簡單一實例進行了介紹,需要的朋友可以過來參考下,希望對大家有所協助

 代碼如下: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雜湊表function HashTable() {     this.ObjArr = {};     this.Count = 0;     //添加    this.Add = function(key, value) {        if (this.ObjArr.hasOwnProperty(key)) {            return false; //如果鍵已經存在,不添加        }        else {            this.ObjArr[key] = value;            this.Count++;            return true;        }    }     //是否包含某項    this.Contains = function(key) {        return this.ObjArr.hasOwnProperty(key);    }     //取某一項 其實等價於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)); //指令碼錯誤            //return;        }    }     //移除    this.Remove = function(key) {        if (this.Contains(key)) {            delete this.ObjArr[key];            this.Count--;        }    }     //清空    this.Clear = function() {        this.ObjArr = {}; this.Count = 0;    }} 測試代碼://員工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); //其實等價於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)); //異常    var result = ht.GetValue(2);    if (result != null) {        alert("Employee Id:" + result.id + ";UserName:" + result.userName);    }    ht.Add(2, "這一個key已經存在!"); //Add無效    //ht.Clear(); //清空    alert(ht.Count); }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.