js字典算法

来源:互联网 发布:淘宝直播主播怎么收费 编辑:程序博客网 时间:2024/05/16 08:14
var Dict = function () {  this.items = [];//用于保存项目};/* * method 添加 * parameter key,value * */Dict.prototype.add = function (key, value) {  this.items[key] = value;};/** method 查找* parameter key* */Dict.prototype.find = function (key) {  return this.items[key];};/* * method 移除 * parameter key * */Dict.prototype.remove = function (key) {  delete this.items[key];};/* * method 查找 * parameter key * */Dict.prototype.display = function () {  for (var key in this.items) {    console.log(key + ' -> ' + this.items[key]);  }};/* * method 数量 * parameter * */Dict.prototype.count = function () {  var count = 0;  for (var key in this.items) {    count++;  }  return count;};/* * method 清楚 * parameter key * */Dict.prototype.clear = function () {  this.items = [];};/*----------------------------------------examples------------------------------------*/if (!module.parent) {  var d = new Dict();  d.add('me', 'Ralph-Wang');  d.add('age', 25);  d.add('job', 'SDT');  d.display();  console.log(d.count());}
0 0
原创粉丝点击