DIV CSS 佈局教程網

自己實作 JavaScript Hashtable:支援忽略大小寫、複製與預設值
編輯:關於JavaScript     

這一版修正了什麼

  • 修掉 hashtableobj.set("length", "0") 的 bug(原本會誤觸陣列長度行為)。
  • 可選擇 key 是否忽略大小寫。
  • 支援 clone 整個 hashtable 物件。
  • 可用 obj.valueOf("key", "defaultvalue") 在取不到值時回傳預設值。

為每個物件加上複製能力

Object.prototype.Clone = function () {
    var objClone;
    if (this.constructor == Object) objClone = new this.constructor();
    else objClone = new this.constructor(this.valueOf());
    for (var key in this) {
        if (objClone[key] != this[key]) {
            if (typeof (this[key]) == 'object') {
                objClone[key] = this[key].Clone();
            } else {
                objClone[key] = this[key];
            }
        }
    }
    objClone.toString = this.toString;
    objClone.valueOf = this.valueOf;
    return objClone;
};

Hashtable 建構器

function Hashtable() {
    this.clear           = hashtable_clear;
    this.containsKey     = hashtable_containsKey;
    this.containsValue   = hashtable_containsValue;
    this.get             = hashtable_get;
    this.isEmpty         = hashtable_isEmpty;
    this.keys            = hashtable_keys;
    this.put             = hashtable_put;
    this.remove          = hashtable_remove;
    this.size            = hashtable_size;
    this.toString        = hashtable_toString;
    this.values          = hashtable_values;
    this.hashtable       = new Object();
    this.set             = hashtable_set;
    this.valueOf         = hashtable_valueOf;
    this.clone           = hashtable_clone;
    this.ignoreupperlower = true;   // 是否忽略大小寫
}

核心方法

所有方法開頭都先做一次大小寫正規化:

function hashtable_put(key, value) {
    if (this.ignoreupperlower && typeof (key) == "string") {
        key = key.toUpperCase();
    }
    if (key == null || value == null) {
        throw "NullPointerException {" + key + "},{" + value + "}";
    } else {
        this.hashtable[key] = value;
    }
}

function hashtable_set(key, value) {   // 有就覆寫,沒有就新增
    if (this.ignoreupperlower && typeof (key) == "string") {
        key = key.toUpperCase();
    }
    if (this.containsKey(key)) { this.remove(key); }
    this.put(key, value);
}

function hashtable_get(key) {
    if (this.ignoreupperlower && typeof (key) == "string") {
        key = key.toUpperCase();
    }
    return this.hashtable[key];
}

function hashtable_valueOf(key, defvalue) {   // 取不到時回傳預設值
    var ret = this.get(key);
    if (typeof (ret) == "undefined") { return defvalue; }
    return ret;
}

function hashtable_remove(key) {
    if (this.containsKey(key)) { delete this.hashtable[key]; }
}

function hashtable_isEmpty() {
    return (parseInt(this.size()) == 0) ? true : false;
}

size 與 containsKey 為何要跳過函式

因為方法本身也掛在 this 上,用 for...in 走訪 this.hashtable 時必須過濾掉函式型別,否則計數會失真:

function hashtable_size() {
    var size = 0;
    for (var i in this.hashtable) {
        if (typeof (this.hashtable[i]) == "function") { continue; }
        if (this.hashtable[i] != null) { size++; }
    }
    return size;
}

function hashtable_containsKey(key) {
    if (this.ignoreupperlower && typeof (key) == "string") {
        key = key.toUpperCase();
    }
    var exists = false;
    for (var i in this.hashtable) {
        if (typeof (this.hashtable[i]) == "function") { continue; }
        if (i == key && this.hashtable[i] != null) { exists = true; break; }
    }
    return exists;
}

keys()、values()、toString()、containsValue() 也沿用同樣的過濾邏輯。

使用範例

function test() {
    var ht = new Hashtable();
    ht.put("3", "Jackson");
    ht.put("2", "Tom");
    ht.put("4", 3);
    ht.set("length", 445555);
    ht.set("ddd", "ddd");
    ht.set("index", "ddd");
    var et = ht.toString();

    ht.ignoreupperlower = false;   // 關閉忽略大小寫
    ht.clear();
    ht.put("3", "Jackson");
    ht.put("2", "Tom");
    ht.remove("2");
    ht.put("4", 3);
    ht.set("length", 5);
    ht.set("index", "ddd");
    ht.set("ddd", "ddd");

    alert(et + "" + ht.toString() + "" + ht.size());
    var cloneobj = ht.clone();
    alert(cloneobj.toString());
}

提醒

把方法掛在 Object.prototype 上會污染所有物件,容易與第三方庫或 for...in 邏輯衝突,正式專案中建議改用獨立命名空間或 ES6 的 Map/Set,它們天生就支援任意型別的鍵與正確的 size。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved