Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HOC, decorating functions
function doSomething(name) {
console.log('hello ' + name);
}
function loggingDecorator(wrappedFunction) {
return function() {
console.log('starting');
const result = wrappedFunction.apply(this, arguments);
console.log('finished');
return result;
}
}
const wrapped = loggingDecorator(doSomething);
doSomething('paul'); // 'hello paul'
wrapped('paul'); // 'starting', 'hello paul', 'finished'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment