-
Notifications
You must be signed in to change notification settings - Fork 3
/
dictionary.html
79 lines (69 loc) · 1.76 KB
/
dictionary.html
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>字典类</title>
</head>
<body>
<h1>字典类</h1>
<p>字典指键值存储的数据结构。可根据键查找对应的值。</p>
<script>
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 this.datastore) {
if (this.datastore.hasOwnProperty(key)) {
console.log(key, ":", this.datastore[key]);
}
}
}
function count() { //字典中的数据计数
var n = 0;
for (var key in this.datastore) {
if (this.datastore.hasOwnProperty(key)) {
n++;
}
}
return n;
}
function clear() { //清空字典
for (var key in this.datastore) {
if (this.datastore.hasOwnProperty(key)) {
delete this.datastore[key];
}
}
}
var pbook = new Dictionary(); //电话号码本
pbook.add("Raymond", "1234567");
pbook.add("David", "345");
pbook.add("Cynthia", "456");
pbook.add("Mike", "723");
pbook.add("Jennifer", "987");
pbook.add("Danny", "012");
pbook.add("Jonathan", "666");
pbook.showAll();
console.log(pbook.count());
pbook.remove("Mike");
console.log(pbook.count());
pbook.clear();
console.log(pbook.count());
pbook.showAll();
</script>
</body>
</html>