欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript實(shí)現(xiàn)字典Dictionary示例基礎(chǔ)

 更新時(shí)間:2023年08月10日 09:39:22   作者:zhoutk  
這篇文章主要為大家介紹了javascript實(shí)現(xiàn)字典Dictionary基礎(chǔ)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

起因

最近在看《數(shù)據(jù)結(jié)構(gòu)與算法--javascript描述》,然后上npmjs.org去搜索,想找合適的庫(kù)參考并記錄下來(lái),以備以后用時(shí)能拿來(lái)即用,最沒(méi)有發(fā)現(xiàn)很合自己意的,于是就決定自己一一實(shí)現(xiàn)出來(lái)。

編程思路

使用了裸對(duì)象datastore來(lái)進(jìn)行元素存儲(chǔ);

實(shí)現(xiàn)了兩種得到字典長(zhǎng)度的方法,一種為變量跟蹤,一種為實(shí)時(shí)計(jì)算。

自己的實(shí)現(xiàn)

(function(){
    "use strict";
    function Dictionary(){
        this._size = 0;
        this.datastore = Object.create(null);
    }
    Dictionary.prototype.isEmpty = function(){
        return this._size === 0;
    };
    Dictionary.prototype.size = function(){
        return this._size;
    };
    Dictionary.prototype.clear = function(){
        for(var key in this.datastore){
            delete this.datastore[key];
        }
        this._size = 0;
    };
    Dictionary.prototype.add = function(key, value){
        this.datastore[key] = value;
        this._size++;
    };
    Dictionary.prototype.find = function(key){
        return this.datastore[key];
    };
    Dictionary.prototype.count = function(){
        var n = 0;
        for(var key in this.datastore){
            n++;
        }
        return n;
    };
    Dictionary.prototype.remove = function(key){
        delete this.datastore[key];
        this._size--;
    };
    Dictionary.prototype.showAll = function(){
        for(var key in this.datastore){
            console.log(key + "->" + this.datastore[key]);
        }
    };
    module.exports = Dictionary;
})();

源代碼地址

https://github.com/zhoutk/js-data-struct 

http://git.oschina.net/zhoutk/jsDataStructs 

以上就是javascript實(shí)現(xiàn)字典Dictionary示例基礎(chǔ)的詳細(xì)內(nèi)容,更多關(guān)于javascript字典Dictionary的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論