TypeScript 实现Dictionary ------别的地方看到的----非原创
1 interface IDictionary { 2 add(key: string, value: any): void; 3 remove(key: string): void; 4 containsKey(key: string): boolean; 5 keys(): string[]; 6 values(): any[]; 7 } 8 9 class Dictionary { 10 11 _keys: string[] = []; 12 _values: any[] = []; 13 14 constructor(init: { key: string; value: any; }[]) { 15 16 for (var x = 0; x < init.length; x++) { 17 this[init[x].key] = init[x].value; 18 this._keys.push(init[x].key); 19 this._values.push(init[x].value); 20 } 21 } 22 23 add(key: string, value: any) { 24 this[key] = value; 25 this._keys.push(key); 26 this._values.push(value); 27 } 28 29 remove(key: string) { 30 var index = this._keys.indexOf(key, 0); 31 this._keys.splice(index, 1); 32 this._values.splice(index, 1); 33 34 delete this[key]; 35 } 36 37 keys(): string[] { 38 return this._keys; 39 } 40 41 values(): any[] { 42 return this._values; 43 } 44 45 containsKey(key: string) { 46 if (typeof this[key] === "undefined") { 47 return false; 48 } 49 50 return true; 51 } 52 53 toLookup(): IDictionary { 54 return this; 55 } 56 }
是ts写的但是没有ts的代码 格式,使用的是js的,,,TypeScript是js的超集。