字典是一种以键值对形式存储数据的数据结构.字典的基础类是Array类,而不是Object类.
function Dictionary(){
this.datastore = new Array()
this.add = add
this.find = find
this.remove = remove
}
function add(key,value){
this.datastore[key] = value
}
function find(key){
return this.datastore[key]
}
this.remove(key){
delete this.datastore[key]
}
查询字典中的元素个数,需要定义一个count 方法。 length属性在这时是不起作用的,因为键的类型是字符串
function count(){
var count = Object.keys(this.datastore).length
return count
}
var nums = new Array()
nums[0] = 1
nums[1] = 2
console.log(nums.length) // 2
var checks = new Array()
checks['name'] = 'Bob'
checks['age'] = '18'
console.log(checks.length) // 0
clear方法,清空字典
function clear(){
for(var key of this.datastore){
delete this.datastore[key]
}
}