1 Dictionary类
字典是一种以键 - 值对形式存储数据的数据结构。
Dictionay 类的基础是 Array 类, 而不是 Object 类。
对字典中的键排序, 而 JavaScript 中是不能对对象的属性进行排序的。
JavaScript 中一切皆对象, 数组也是对象。
//定义类
function Dictionary() {
this.add = add;
this.datastore = new Array();
this.find = find;
this.remove = remove;
this.showAll = showAll;
}
//增加
function add(key, value) {
this.datastore[key] = value;
}
//查找
function find(key) {
return this.datastore[key];
}
//移除
function remove(key) {
delete this.datastore[key];
}
//显示
function showAll() {
for(var key in Object.keys(this.datastore)) {
print(key + " -> " + this.datastore[key]);
}
}
2 Dictionary类的辅助方法
//字典中的元素个数
//当键的类型为字符串时, length 属性就不管用了
function count() {
var n = 0;
for each (var key in Object.keys