Skip to content

Latest commit

 

History

History
57 lines (52 loc) · 1013 Bytes

06.md

File metadata and controls

57 lines (52 loc) · 1013 Bytes

字典

字典是什么

  1. 字典是以键值对形式存储数据的数据结构

字典的实现

function Dictionary() {
  this.dataStore = new Array();
  this.add = add;
  this.find = find;
  this.remove = remove;
  this.showAll = showAll;
  this.count = count;
  this.clear = clear;
}
var add = function(key, value) {
  this.dataStore[key] = value;
};
var find = function(key) {
  return this.dataStore[key];
};
var remove = function(key) {
  delete this.dataStore[key];
};
var showAll = function() {
  var result = "";
  for (var key in this.dataStore) {
    result += key + ":" + this.dataStore[key] + ",";
  }
  console.log(result);
};
var count = function() {
  var count = 0;
  for (var key in this.dataStore) {
    count++;
  }
  return count;
};
var clear = function() {
  this.dataStore = new Array();
};

var d = new Dictionary();
d.add("a", 1);
d.add("b", 2);
d.add("c", 3);
d.add("d", 4);
d.showAll();
d.remove("d");
d.showAll();
console.log(d.count());
d.clear();
d.showAll();