Chapter 11, Implementing Design Patterns – The Functional Way
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 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}`);
...