Created
May 3, 2018 08:45
-
-
Save brownsmith/0647d33dc02ae18b6b43d3dac3634b66 to your computer and use it in GitHub Desktop.
HOC, decorating functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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