July 2019
Intermediate to advanced
416 pages
10h 6m
English
One of the features we want to use in our client code is to be able to log method calls, along with the parameters that are passed into them. We have encountered this type of feature before when we looked at creating decorators. In this case, we want to create a Log decorator:
export function Log() { return function(target: Object, propertyName: string, propertyDesciptor: PropertyDescriptor): PropertyDescriptor { const method = propertyDesciptor.value; propertyDesciptor.value = function(...args: unknown[]) { const params = args.map(arg => JSON.stringify(arg)).join(); const result = method.apply(this, args); if (args && args.length > 0) { console.log(`Calling ${propertyName} with ${params}`); } ...
Read now
Unlock full access