
You, probably, have already heard about such a strange thing for a js / ts developer, like decorators . In general, this is a design pattern that can be used in any language. But some programming languages, such as python, dragged this pattern into their syntax, which caused a controversial reaction among developers. TypeScript has already established itself as forty, pulling a good syntax from different programming languages. But will decorators benefit him?
Today I will show you a whole factory and one great case study using decorators from our new SDK.
Let's see what we really need decorators for:
// tsconfig.json { "compilerOptions": { "target": "ES5", "experimentalDecorators": true } }  //   .      trace //      class TraceExperiment{ private isTrace: boolean = true; //        ,   //  . ,      ,  //   ,     function traceMe(functionName: string, parameters: string, result: string): void { if (this.isTrace) console.log( new Date().toString() + ` TRACE: ${functionName}(${parameters}) => ${result}`); } //          function doWork(param_one: any, param_two: any): any{ let result:any; //      this.traceMe(arguments.callee.name,arguments.join(),result); }   @Logger.trace("MODULE1") function doWork(param_one: any, param_two: any): any { //      }  //   public static trace(category:string) { //          // //   //     // propertyKey -    // descriptor -    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { return { //    .    //   ,       value: function (...args: any[]) { //      let params = ""; //   ,       try { params = args.map(a => JSON.stringify(a)).join(); } catch (e) { params = "[circular structure]"; } //   ,    // ,      let result = descriptor.value.apply(this, args); let r; //   ,       try { r = JSON.stringify(result); } catch (e) { r = "[circular structure]"; } //      Logger.traceMe(propertyKey, params, r); //   ,     return result; } }; } }   __decorate([Logger_1.LogManager.d_trace(Logger_1.LogCategory.RTC)], SeriousModule.prototype, "doWork", null); Source: https://habr.com/ru/post/277847/
All Articles