Skip to content

Instantly share code, notes, and snippets.

@RickWong
Created August 8, 2015 10:08
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 RickWong/4968d3636fff3849ea48 to your computer and use it in GitHub Desktop.
Save RickWong/4968d3636fff3849ea48 to your computer and use it in GitHub Desktop.
ES6 function decorators
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