字典 Dictionary类
<span style="color: #0000ff;">this.datastore[key] =<span style="color: #000000;"> value;
} <span style="color: #0000ff;">function<span style="color: #000000;"> find(key) {
<span style="color: #0000ff;">return <span style="color: #0000ff;">this<span style="color: #000000;">.datastore[key];
} <span style="color: #0000ff;">function<span style="color: #000000;"> remove(key) {
<span style="color: #0000ff;">delete <span style="color: #0000ff;">this<span style="color: #000000;">.datastore[key];
} <span style="color: #0000ff;">function<span style="color: #000000;"> showAll() {
<span style="color: #0000ff;">var str = ""<span style="color: #000000;">;
<span style="color: #0000ff;">for(<span style="color: #0000ff;">var key <span style="color: #0000ff;">in <span style="color: #0000ff;">this<span style="color: #000000;">.datastore) {
str += key + " -> " + <span style="color: #0000ff;">this.datastore[key] + "; "<span style="color: #000000;">
}
console.log(str);
}
<span style="color: #0000ff;">function<span style="color: #000000;"> count() {
<span style="color: #008000;">/<span style="color: #008000;">var ss = Object.keys(this.datastore).length;
console.log("ssss "+ss);
return Object.keys(this.datastore).length;<span style="color: #008000;">/
<span style="color: #008000;">/**/
<span style="color: #0000ff;">var n = 0<span style="color: #000000;">;
<span style="color: #0000ff;">for(<span style="color: #0000ff;">var key <span style="color: #0000ff;">in Object.keys(<span style="color: #0000ff;">this<span style="color: #000000;">.datastore)) {
++<span style="color: #000000;">n;
}
console.log(n);
<span style="color: #0000ff;">return<span style="color: #000000;"> n;
}
<span style="color: #0000ff;">function<span style="color: #000000;"> clear() {
<span style="color: #0000ff;">for(<span style="color: #0000ff;">var key <span style="color: #0000ff;">in <span style="color: #0000ff;">this<span style="color: #000000;">.datastore) {
<span style="color: #0000ff;">delete <span style="color: #0000ff;">this<span style="color: #000000;">.datastore[key];
}
}
<span style="color: #0000ff;">var pbook = <span style="color: #0000ff;">new<span style="color: #000000;"> Dictionary();
pbook.add("Mike","723"<span style="color: #000000;">);
pbook.add("Jennifer","987"<span style="color: #000000;">);
pbook.add("Jonathan","666"<span style="color: #000000;">);
pbook.showAll();<span style="color: #008000;">//<span style="color: #008000;">Mike -> 723; Jennifer -> 987; Jonathan -> 666;
pbook.count();<span style="color: #008000;">//<span style="color: #008000;">3
pbook.remove("Jennifer"<span style="color: #000000;">);
<span style="color: #008000;">//<span style="color: #008000;">pbook.clear();
pbook.showAll();<span style="color: #008000;">//<span style="color: #008000;">Mike -> 723; Jonathan -> 666;
pbook.count();<span style="color: #008000;">//<span style="color: #008000;">2