function add(key, value){
this.dataStore[key] = value;
function show(){
for(var key in this.dataStore){
console.log(key + " : " + this.dataStore[key]);
function find(key){
return this.dataStore[key];
function remove(key){
delete this.dataStore[key];
function count(){
var n = 0;
for(var key in Object.keys(this.dataStore)){
return n;
function kSort(){
var dic = this.dataStore;
var res = Object.keys(dic).sort();
for(var key in res ){
console.log(res[key] + " : " + dic[res[key]]);
function vSort(){
var dic = this.dataStore;
var res = Object.keys(dic).sort(function(a,b){
return dic[a]-dic[b];
for(var key in res ){
console.log(res[key] + " : " + dic[res[key]]);
function clear(){
for(var key in this.dataStore){
delete this.dataStore[key];
function Dictionary(){
this.dataStore = new Array();
this.add = add;
this.show = show;
this.find = find;
this.remove = remove;
this.count = count;
this.kSort = kSort;
this.vSort = vSort;
this.clear = clear;
var dic = new Dictionary();
dic.add('one', '1');
dic.add('three', '3');
dic.add('two', '2');
dic.add('8', 'seven');
dic.add('five', '5');
dic.add('four', '4');
dic.add('9', 'nine');
dic.add('six', '6');
dic.add('7', 'eight');
dic.show();
运行结果:
console.log("one: " + dic.find("one"))
console.log("7: " + dic.find("7"))
运行结果:
dic.remove("9");
运行结果:
console.log(dic.count());
运行结果:
dic.kSort();
运行结果:
dic.vSort();
运行结果:
dic.clear();
那么,有关字典这种数据结构的学习和使用就到此了,如果在后续使用到有加深的地方,再补充吧~~
vickie
JavaScript