1 1 interface IDictionary { 2 2 add(key: string, value: any): void; 3 3 remove(key: string): void; 4 4 containsKey(key: string): boolean; 5 5 keys(): string[]; 6 6 values(): any[]; 7 7 } 8 8 9 9 class Dictionary { 1010 1111 _keys: string[] = []; 1212 _values: any[] = []; 1313 1414 constructor(init: { key: string; value: any; }[]) { 1515 1616 for (var x = 0; x < init.length; x++) { 1717 this[init[x].key] = init[x].value; 1818 this._keys.push(init[x].key); 1919 this._values.push(init[x].value); 2020 } 2121 } 2222 2323 add(key: string, value: any) { 2424 this[key] = value; 2525 this._keys.push(key); 2626 this._values.push(value); 2727 } 2828 2929 remove(key: string) { 3030 var index = this._keys.indexOf(key, 0); 3131 this._keys.splice(index, 1); 3232 this._values.splice(index, 1); 3333 3434 delete this[key]; 3535 } 3636 3737 keys(): string[] { 3838 return this._keys; 3939 } 4040 4141 values(): any[] { 4242 return this._values; 4343 } 4444 4545 containsKey(key: string) { 4646 if (typeof this[key] === "undefined") { 4747 return false; 4848 } 4949 5050 return true; 5151 } 5252 5353 toLookup(): IDictionary { 5454 return this; 5555 } 5656 }
是ts写的但是没有ts的代码 格式,使用的是js的,,,TypeScript是js的超集。