Decorators can inject additional logic into classes or methods without changing the core logic of the code.

Decorates are only available in TypeScript. In JavaScript, their functionality is quite different.

Class-decorators differ from decorators on methods.

// class decorator 
function logged<T extends { new (...args: any[]): {} }>(constructor: T) {
  return class extends constructor {
    constructor(...args: any[]) {
      console.log(`Creating new instance of ${constructor.name} with arguments: ${JSON.stringify(args)}`);
      super(...args);
    }
  };
}
 
@logged
class Calculator {
  constructor(public name: string) {}
 
  add(a: number, b: number): number {
    return a + b;
  }
}
 
const calc = new Calculator("MyCalculator");
calc.add(5, 3);