¶Ô±ÈÐÂÎļþ |
| | |
| | | /** |
| | | * å建ä¸ä¸ªå¯¹è±¡ |
| | | */ |
| | | export class Create<T> { |
| | | constructor(args: T) { |
| | | this.init(args) |
| | | } |
| | | |
| | | [key: string]: any |
| | | |
| | | /** |
| | | * åå§å对象ï¼è¦æ±ç»æ[[key, value]] æ { key: value } |
| | | * |
| | | * new Create([1,2]) or new Create({ 1: 2 }) |
| | | * @param args |
| | | */ |
| | | private init(args: T) { |
| | | if (Array.isArray(args)) { |
| | | args.forEach(([key, value = '']) => { |
| | | this[key] = value |
| | | }) |
| | | } else if (args instanceof Object) { |
| | | Object.entries(args).forEach(([key, value = '']) => { |
| | | this[key] = value |
| | | }) |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è®¾ç½®å¼ |
| | | * @param key |
| | | * @param value |
| | | * @returns void |
| | | */ |
| | | set(key: string, value: any) { |
| | | return (this[key] = value) |
| | | } |
| | | /** |
| | | * è·åå¼ |
| | | * @param key |
| | | * @returns any |
| | | */ |
| | | get(key: string) { |
| | | return this[key] |
| | | } |
| | | |
| | | /** |
| | | * å é¤å¼ |
| | | * @param key |
| | | */ |
| | | remove(key: string) { |
| | | delete this[key] |
| | | } |
| | | /** |
| | | * æ·»å å¼ |
| | | * @param key |
| | | * @param value |
| | | * @returns |
| | | */ |
| | | insert(key: string, value: any) { |
| | | return (this[key] = value) |
| | | } |
| | | |
| | | /** |
| | | * æ´æ°æ°æ® |
| | | * @param o |
| | | * @returns |
| | | */ |
| | | update(o: T) { |
| | | this.init(o) |
| | | } |
| | | /** |
| | | * é置对象 |
| | | */ |
| | | reset() { |
| | | Object.entries(this).forEach(([key, value]: string[]) => { |
| | | if (typeof this[key] !== 'function') { |
| | | this.remove(key) |
| | | } |
| | | }) |
| | | } |
| | | } |