Coggle requires JavaScript to display documents.
class Product { @Log1 title: string; private _price: number; set price(val: number) { if (val > 0) { this._price = val; } else { throw new Error('Price must be positive'); } } constructor(t: string, p: number) { this.title = t; this._price = p; } getPriceWithTax(@Log4 tax: number) { return this._price * (1 + tax); } }
function Log1(target: any, propertyName: string | Symbol) { console.log('Property decorator!'); console.log(target, propertyName); }
> Property decorator! > {constructor: ƒ, getPriceWithTax: ƒ} 'title'
class Product { . . . @Log2 set price(val: number) { if (val > 0) { this._price = val; } else { throw new Error('Price must be positive'); } } . . . }
function Log2( target: any, name: string | Symbol, descriptor: PropertyDescriptor ) { console.log('Accessor decorator!'); console.log(target); console.log(name); console.log(descriptor); }
> Accessor decorator! > {constructor: ƒ, getPriceWithTax: ƒ} > price > {get: undefined, enumerable: false, configurable: true, set: ƒ}
price
_price
function Log3( target: any, name: string | Symbol, descriptor: PropertyDescriptor ) { console.log('Method decorator!'); console.log(target); console.log(name); console.log(descriptor); }
class Product { . . . @Log3 getPriceWithTax(tax: number) { return this._price * (1 + tax); } . . . }
> Method decorator! > {constructor: ƒ, getPriceWithTax: ƒ} > getPriceWithTax > {writable: true, enumerable: false, configurable: true, value: ƒ}
class Product { . . . getPriceWithTax(@Log4 tax: number) { return this._price * (1 + tax); } . . . }
function Log4(target: any, name: string | Symbol, position: number) { console.log('Parameter decorator!'); console.log(target); console.log(name); console.log(position); }
> Parameter decorator! > {constructor: ƒ, getPriceWithTax: ƒ} > getPriceWithTax > 0