常用方法:
- set(key, value) 添加键值对
- has(key)判断字典中是否有某个key
- remove(key)从字典中移除元素remove
- get(key) 根据key去获取value
- keys() 获取多有的keys
- values()获取所有的value
- size() 查看大小
- clear()方法
// 封装字典
// 创建字典的构造函数
function Dictionary() {
// 字典属性
this.items = {}
// 字典操作方法
// 1 添加键值对
Dictionary.prototype.set = function (key, value) {
this.items[key] = value;
}
// 2 判断字典中是否有某个key
Dictionary.prototype.has = function (key) {
return this.items[key] != null
}
// 3 从字典中移除元素remove
Dictionary.prototype.remove = function (key) {
delete this.items[key]
return true
}
// 4 根据key去获取value
Dictionary.prototype.get = function (key) {
return this.has(key) ? this