//字典数据结构
function Dictionary(){
this.items = {};
//检查是否有某一个键
this.has = function(key){
return this.items.hasOwnProperty(key);
}
//为字典添加某一个值
this.set = function(key,val){
this.items[key] = val;
}
//删除某一个键
this.delete = function(key){
if(this.has(key)){
delete this.items[key];
return true;
}
return false;
}
//查找某一特定项
this.get = function(key){
return this.has(key) ? this.items[key] : undefined;
}
//返回字典中的所有值
this.values = function(){
var res = [];
for(var prop in this.items){
if(this.has(prop)){
res.push(this.items[prop]);
}
}
return res;
}
//清空字典
this.clear = function(){
this.items = {};
}
//获取字典的长度
this.size = function(){
return Object.keys(this.items).length;
}
//获取字典所有的键
this.keys = function(){
return Object.keys(this.items);
}
//返回字典本身
this.getItems = function(){
js实现字典结构
最新推荐文章于 2024-01-12 07:45:00 发布