javascript实现字典Dictionary示例基础
起因
最近在看《数据结构与算法--javascript描述》,然后上npmjs.org去搜索,想找合适的库参考并记录下来,以备以后用时能拿来即用,最没有发现很合自己意的,于是就决定自己一一实现出来。
编程思路
使用了裸对象datastore来进行元素存储;
实现了两种得到字典长度的方法,一种为变量跟踪,一种为实时计算。
自己的实现
(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实现字典Dictionary示例基础的详细内容,更多关于javascript字典Dictionary的资料请关注脚本之家其它相关文章!
相关文章
jQuery单页面文字搜索插件jquery.fullsearch.js的使用方法
jquery.fullsearch.js是一款基于Bootstrap文字搜索插件,可以帮助您快速搜索到当前页面所包含的指定文字,并定位到所在位置2020-02-02
javascript使用btoa和atob来进行Base64转码和解码
javascript原生的api本来就支持,Base64,但是由于之前的javascript局限性,导致Base64基本中看不中用。当前html5标准正式化之际,Base64将有较大的转型空间,对于Html5 Api中出现的如FileReader Api, 拖拽上传,甚至是Canvas,Video截图都可以实现2017-03-03
一文了解JavaScript用Element Traversal新属性遍历子元素
这篇文章主要介绍了一文了解JavaScript用Element Traversal新属性遍历子元素,文章围绕Element Traversal新属性的相关资料展开详细内容,需要的朋友可以参考一下,希望对大家有所帮助2021-11-11


最新评论