/* * 算法——字典 * 字典是一种以键 - 值对形式存储数据的数据结构,Dictionay 类的基础是 Array 类。数组当对象用??? * add: 增加元素 * find:查找元素 * remove:删除元素 * */ function add(key,value) { this.dataStore[key] = value; } function find(key) { return this.dataStore[key] } function remove(key) { delete this.dataStore[key]; } //sort 能够有序输出 叫有序字典 function showAll() { var keysArray = Object.keys(this.dataStore).sort(); for (var i = 0 ; i < keysArray.length;i++) { console.log(keysArray[i] + "=====>" + this.dataStore[keysArray[i]]); } } function count() { var n = 0; for (var key in this.dataStore) { n++; } return n; } function clear() { for (var key in this.dataStore) { delete this.dataStore[key]; } } function Dictionay() { this.dataStore = []; this.add = add; this.find = find; this.remove = remove; this.showAll = showAll; this.count = count; this.clear = clear; } var d = new Dictionay(); d.add("name","张三"); d.add("phone","15019222409"); d.showAll(); console.log(d.count())