February 2019
Beginner
694 pages
18h 4m
English
Since we have the definition of a function available to us within a method decorator, we could use the decorator to modify the functionality of a specific function. Suppose that we wanted to create an audit trail of some sort, and log a message to the console every time a method was called. This is the perfect scenario for method decorators.
Consider the following method decorator:
function auditLogDec(target: any, methodName: string, descriptor?: PropertyDescriptor) { let originalFunction = target[methodName]; let auditFunction = function (this: any) { console.log(`auditLogDec : overide of ` + ` ${methodName} called`); for (let i = 0; i < arguments.length; i++) { console.log(`arg : ${i} = ${arguments[i]}`); } originalFunction.apply(this, ...Read now
Unlock full access