-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05.dictionary.js
More file actions
58 lines (50 loc) · 1.13 KB
/
05.dictionary.js
File metadata and controls
58 lines (50 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Created by lei.sun on 2017/8/13.
*/
// 字典
function Dictionary() {
this.add = add;
this.datastore = new Array();
this.find = find;
this.remove = remove;
this.showAll = showAll;
this.count = count;
this.clear = clear;
}
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).sort()) {
console.log(key + " -> " + this.datastore[key]);
}
}
function count() {
var n = 0;
for (var key in Object.keys(this.datastore)) {
++n;
}
return n;
}
function clear() {
for (var key in Object.keys(this.datastore)) {
delete this.datastore[key];
}
}
// 测试
var pbook = new Dictionary();
pbook.add("Raymond", "123");
pbook.add("David", "345");
pbook.add("Cynthia", "456");
console.log("Number of entries:" + pbook.count());
console.log("David is extension:" + pbook.find("David"));
pbook.showAll();
pbook.clear();
console.log("Number of entries:" + pbook.count());
// 键值对