-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.js
More file actions
54 lines (43 loc) · 1.24 KB
/
Dictionary.js
File metadata and controls
54 lines (43 loc) · 1.24 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
function Dictionary () {
this.dataStore = {};
this.add = add;
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() {
var sorted = (function(s){var t={};Object.keys(s).sort().forEach(function(k){t[k]=s[k]});return t})(this.dataStore);
for(var k in sorted)
console.log(k, '->', sorted[k]);
}
function count () { // 문자열 key로 이뤄진 배열에선 .length 프로퍼티가 오작동
var n = 0;
for(var key in this.dataStore)
n++;
return n
}
function clear () {
for(var key in this.dataStore)
delete this.dataStore[key];
}
}
var pbook = new Dictionary();
pbook.add("Mike", '123');
pbook.add("David", '345');
pbook.add("Cynthia", '456');
console.log("Number of entries:", pbook.count());
console.log("David's extension:", pbook.find('David'));
pbook.showAll();
console.log();
pbook.clear();
console.log("Number of entries ", pbook.count());