JavaScript 字典(Dictionary)

TypeScript方式实现源码

//  set(key,value):向字典中添加新元素。 
//  remove(key):通过使用键值来从字典中移除键值对应的数据值。 
//  has(key):如果某个键值存在于这个字典中,则返回true,反之则返回false。 
//  get(key):通过键值查找特定的数值并返回。 
//  clear():将这个字典中的所有元素全部删除。 
//  size():返回字典所包含元素的数量。与数组的length属性类似。 
//  keys():将字典所包含的所有键名以数组形式返回。 
//  values():将字典所包含的所有数值以数组形式返回。 
 1 /**
 2  * 字典
 3  * @desc 与Set类相似,ECMAScript 6同样包含了一个Map类的实现,即我们所说的字典
 4  */
 5 class Dictionary {
 6     private items = {};
 7     public set(key, value) {
 8         this.items[key] = value;
 9     }
10     public remove(key) {
11         if (this.has[key]) {
12             delete this.items[key];
13             return true;
14         } else {
15             return false;
16         }
17     }
18     public has(key) {
19         return key in this.items;
20     }
21     public get(key) {
22         return this.has(key) ? this.items[key] : undefined;
23     }
24     public clear() {
25         this.items = {};
26     }
27     public size() {
28         var count = 0;
29         for (var prop in this.items) { //{5} 
30             if (this.items.hasOwnProperty(prop)) //{6} 
31                 ++count; //{7} 
32         }
33         return count;
34     }
35     public keys() {
36         let values = [];
37         for (var k in this.items) {
38             if (this.has(k)) {
39                 values.push(k);
40             }
41         }
42         return values;
43     }
44     public values(): Array<any> {
45         let values = [];
46         for (var k in this.items) {
47             if (this.has(k)) {
48                 values.push(this.items[k]);
49             }
50         }
51         return values;
52     }
53     public getItems() {
54         return this.items;
55     }
56 }
字典 Dictionary
// 使用我们创建的类来执行如下代码:
var dictionary = new Dictionary();
dictionary.set('Gandalf', 'gandalf@email.com');
dictionary.set('John', 'johnsnow@email.com');
dictionary.set('Tyrion', 'tyrion@email.com');
// 如果执行了如下代码,输出结果将会是true:
console.log(dictionary.has('Gandalf'));
// 下面的代码将会输出3,因为我们向字典实例中添加了三个元素:
console.log(dictionary.size());
// 现在,执行下面的几行代码:
console.log(dictionary.keys());
console.log(dictionary.values());
console.log(dictionary.get('Tyrion'));
// 输出结果分别如下所示:
// ["Gandalf", "John", "Tyrion"]
// ["gandalf@email.com", "johnsnow@email.com", "tyrion@email.com"]
// tyrion@email.com
// 最后,再执行几行代码:
dictionary.remove('John');
// 再执行下面的代码: 
console.log(dictionary.keys());
console.log(dictionary.values());
console.log(dictionary.getItems());
// 输出结果如下所示: 
// ["Gandalf", "Tyrion"] 
// ["gandalf@email.com", "tyrion@email.com"] 
// Object {Gandalf: "gandalf@email.com", Tyrion: "tyrion@email.com"} 
// 移除了一个元素后, 现在的dictionary实例中只包含两个元素了。 加粗的一行表现了items
// 对象的内部结构。
使用方式

JavaScript方式实现源码

 1 /**
 2  * 字典
 3  * @desc 与Set类相似,ECMAScript 6同样包含了一个Map类的实现,即我们所说的字典
 4  */
 5 var Dictionary = (function () {
 6     function Dictionary() {
 7         this.items = {};
 8     }
 9     Dictionary.prototype.set = function (key, value) {
10         this.items[key] = value;
11     };
12     Dictionary.prototype.remove = function (key) {
13         if (this.has[key]) {
14             delete this.items[key];
15             return true;
16         }
17         else {
18             return false;
19         }
20     };
21     Dictionary.prototype.has = function (key) {
22         return key in this.items;
23     };
24     Dictionary.prototype.get = function (key) {
25         return this.has(key) ? this.items[key] : undefined;
26     };
27     Dictionary.prototype.clear = function () {
28         this.items = {};
29     };
30     Dictionary.prototype.size = function () {
31         var count = 0;
32         for (var prop in this.items) {
33             if (this.items.hasOwnProperty(prop))
34                 ++count; //{7} 
35         }
36         return count;
37     };
38     Dictionary.prototype.keys = function () {
39         var values = [];
40         for (var k in this.items) {
41             if (this.has(k)) {
42                 values.push(k);
43             }
44         }
45         return values;
46     };
47     Dictionary.prototype.values = function () {
48         var values = [];
49         for (var k in this.items) {
50             if (this.has(k)) {
51                 values.push(this.items[k]);
52             }
53         }
54         return values;
55     };
56     Dictionary.prototype.getItems = function () {
57         return this.items;
58     };
59     return Dictionary;
60 }());
字典 Dictionary
原文地址:https://www.cnblogs.com/menu/p/6973040.html