11.1. Decorating methods, the future way: As we've already mentioned, decorators aren't a fixed, definitive feature at the moment. However, by following https://tc39.github.io/proposal-decorators/, we can write the following:
const logging = (target, name, descriptor) => {
const savedMethod = descriptor.value;
descriptor.value = function(...args) {
console.log(`entering ${name}: ${args}`);
try {
const valueToReturn = savedMethod.bind(this)(...args);
console.log(`exiting ${name}: ${valueToReturn}`);
return valueToReturn;
} catch (thrownError) {
console.log(`exiting ${name}: threw ${thrownError}`);
throw thrownError;
}
};
return descriptor;
};
We want to add a @logging decoration to a method. We save the original method in savedMethod and substitute a new...