Skip to content

Instantly share code, notes, and snippets.

@brownsmith
Created May 3, 2018 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brownsmith/0647d33dc02ae18b6b43d3dac3634b66 to your computer and use it in GitHub Desktop.
Save brownsmith/0647d33dc02ae18b6b43d3dac3634b66 to your computer and use it in GitHub Desktop.
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