javascript實(shí)現(xiàn)字典Dictionary示例基礎(chǔ)
起因
最近在看《數(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)文章
document 和 document.all 分別什么時(shí)候用
document 和 document.all 分別什么時(shí)候用...2006-06-06JS前端可視化canvas動(dòng)畫(huà)原理及其推導(dǎo)實(shí)現(xiàn)
這篇文章主要為大家介紹了JS前端可視化canvas動(dòng)畫(huà)原理及其推導(dǎo)實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08微信小程序 使用picker封裝省市區(qū)三級(jí)聯(lián)動(dòng)實(shí)例代碼
這篇文章主要介紹了微信小程序 使用picker封裝省市區(qū)三級(jí)聯(lián)動(dòng)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10微信小程序 scroll-view實(shí)現(xiàn)上拉加載與下拉刷新的實(shí)例
這篇文章主要介紹了微信小程序 scroll-view實(shí)現(xiàn)上拉加載與下拉刷新的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01微信小程序頁(yè)面開(kāi)發(fā)注意事項(xiàng)整理
這篇文章主要介紹了微信小程序頁(yè)面開(kāi)發(fā)注意事項(xiàng)整理的相關(guān)資料,需要的朋友可以參考下2017-05-05