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
| 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());
|