January 2020
Intermediate to advanced
470 pages
11h 13m
English
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 ...
Read now
Unlock full access