javascript類hash表 類的定義

來源:互聯網
上載者:User
//Hash.js//定義Hash的javascript類var Hash = function (h) {    this._data = new Object();}function Hash$clear() {    delete this._data;    this._data = new Object();}function Hash$add(key, value) {    if (!key || typeof (value) === 'undefined') return false;    this._data[key] = { key: key, value: value };    return true;}function Hash$addRange(h) {    for (var key in h._data) {        var item = h._data[key];        if (typeof (item) !== 'undefined') {            this.add(item.key, item.value);        }    }}function Hash$remove(key) {    if (!key) return undefined;    var item = this._data[key];    delete this._data[key];    return item;}function Hash$removeAt(index) {    if (isNaN(index)) return undefined;    var i = 0;    for (var key in this._data) {        if (i == index) {            return this.remove(key);        }        i++;    }    return undefined;}function Hash$removeRange(startIndex, endIndex) {    if (isNaN(startIndex) || isNaN(endIndex)) return undefined;    var i = 0;    var h = new Hash();    for (var key in this._data) {        if (i >= startIndex && i <= endIndex) {            h.add(key, this.remove(key).value);        }        i++;    }    return h;}function Hash$getCount() {    var i = 0;    for (var key in this._data) i++;    return i;}function Hash$forEach(method, instance) {    var i = 0;    for (var key in this._data) {        var item = this._data[key];        if (typeof (item) !== 'undefined') {            method.call(instance, item, i, this);            i++;        } else {            delete this._data[key];        }    }}function Hash$getKeys() {    var arr = new Array();    for (var key in this._data) {        var item = this._data[key];        arr.push(item.key);    }    return arr;}function Hash$getValues() {    var arr = new Array();    for (var key in this._data) {        var item = this._data[key];        arr.push(item.value);    }    return arr;}function Hash$getItem(key) {    if (!key) return undefined;    var item = this._data[key];    if (typeof (item) !== 'undefined') {        return item;    } else {        delete this._data[key];        return undefined;    }}function Hash$containsKey(key) {    if (typeof (this.getItem(key)) !== 'undefined') {        return true;    }    return false;}function Hash$containsValue(value) {    for (var key in this._data) {        if (value === this._data[key].value) {            return true;        }    }    return false;}//新增加的加入一個數組自動計算key的個數函數function Hash$addArray(s) {    //統計個數    var vc = 0;    var item = null;    for (var i = 0; i < s.length; i++) {        if (this.containsKey(s[i]) == false) {            this.add(s[i], 1);        }        else {            item = this.getItem(s[i]);            vc = item.value;            vc += 1;            this.remove(s[i]);            this.add(s[i], vc);        }    }}//擷取key=(value)對的顯示測試資訊function Hash$getHashKeyValueInfo() {    //顯示    var inf = '';    var keyArray = this.getKeys();    var valArray = this.getValues();    for (var i = 0; i < keyArray.length; i++) {        inf += keyArray[i] + '=(' + valArray[i] + "),";    }    return inf;}//擷取key對應的值function Hash$getValue(key) {    var item = this.getItem(key);    var vc = item.value;    return vc;}//原型定義Hash.prototype = {    _data: null,    _keys: null,    clear: Hash$clear,    add: Hash$add,    addRange: Hash$addRange,    remove: Hash$remove,    removeAt: Hash$removeAt,    removeRange: Hash$removeRange,    getCount: Hash$getCount,    forEach: Hash$forEach,    getKeys: Hash$getKeys,    getValues: Hash$getValues,    getItem: Hash$getItem,    containsKey: Hash$containsKey,    containsValue: Hash$containsValue,    addArray: Hash$addArray,    getHashKeyValueInfo: Hash$getHashKeyValueInfo,    getValue: Hash$getValue}Hash.__typeName = 'Hash';Hash.__class = true;
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.