Created
August 8, 2015 10:08
-
-
Save RickWong/4968d3636fff3849ea48 to your computer and use it in GitHub Desktop.
ES6 function decorators
This file contains hidden or 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 decorate (...functions) { | |
return functions.reduce( | |
(a, b) => b(a), | |
functions.pop() // Pop wrapped function as initial value | |
); | |
}; | |
decorate( | |
(func) => (text) => func(text.toUpperCase() + "!"), // Outer-wrapper | |
(func) => (text) => func(text.toLowerCase() + "…"), // Inner-wrapper | |
(text) => console.log(text) // Wrapped | |
)( | |
"Hello World" | |
); | |
// Output: "HELLO WORLD…!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment