JavaScript数据结构——字典(Dictionary)

时间:2024-10-01 20:14:33
var Dictionary = function(){ var dictionary = {}; //检查键是否存在 = function(key){ return (key); } //添加元素 = function(key , value){ dictionary[key] = value; } //通过键移除元素 = function(key){ if((key)){ delete dictionary[key]; return true; }else{ return false; } } //由键获取值 = function(key){ if((key)){ return dictionary[key]; }else{ return undefined; } } //提取字典所有值 = function(){ var values = []; for(var key in dictionary){ if((key)){ (dictionary[key]); } } return values; } //获取全部键名 = function(){ return (dictionary); } //清除字典 = function(){ dictionary = {}; } //获取字典长度 = function(){ return (dictionary).length; } }