首頁  >  文章  >  web前端  >  javascript實作的HashMap類別程式碼_javascript技巧

javascript實作的HashMap類別程式碼_javascript技巧

WBOY
WBOY原創
2016-05-16 16:42:551699瀏覽

複製程式碼程式碼如下:


函數 HashMap() {
    /**Map大小**/
    變數大小 = 0;
    /**物件**/
    var Entry = new Object();
    /**Map的存put方法**/
    this.put = 函數(鍵, 值) {
        if (!this.containsKey(key)) {
            尺寸;
            條目[鍵] = 價值;
        }
    }
    /**Map取get方法**/
    this.get = 函數(key) {
        回 this.containsKey(key) ?條目[鍵]:空;
    }
    /**Map刪除remove方法**/
    this.remove = 函數(key) {
        if (this.containsKey(key) && (刪除條目[key])) {
            尺寸--;
        }
    }
    /**是否包含Key**/
    this.containsKey = 函數(鍵) {
        return(輸入條目);
    }
    /**是否包含Value**/
    this.containsValue = 函數(值) {
        for (條目中的 var 屬性) {
            if (entry[prop] == value) {
                回復為真;
            }
        }
        回傳錯誤;
    }
    /**所有的Value**/
    this.values = function() {
        var value = new Array();
        for (條目中的 var 屬性) {
            value.push(entry[prop]);
        }
        回傳值;
    }
    /**所有的 Key**/
    this.keys = function() {
        var key = new Array();
        for (條目中的 var 屬性) {
            鍵.push(prop);
        }
        返回鍵;
    }
    /**地圖尺寸**/
    this.size = function() {
        返回大小;
    }
    /**清空Map**/
    this.clear = function() {
        大小 = 0;
        條目 = new Object();
    }

}
//建立HashMap物件
var hashMap = new HashMap();
hashMap.put("A", "1");
hashMap.put("B", "2");
hashMap.put("A", "5");
hashMap.put("C", "3");
hashMap.put("A", "4");
警報(hashMap.size());

腳本>

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