Skip to content

Instantly share code, notes, and snippets.

@UniBreakfast
Created January 29, 2023 12:30
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 UniBreakfast/33923d32d1b74ef229c4740936e1b0d9 to your computer and use it in GitHub Desktop.
Save UniBreakfast/33923d32d1b74ef229c4740936e1b0d9 to your computer and use it in GitHub Desktop.
logDecorator makes decorated function that logs to the console function name, arguments and result.
sub(mult(2, 5), add(2, div(12, 3))) // 4
function sub(x, y){
return x - y
}
function mult(x, y){
return x * y
}
function div(x, y){
return x / y
}
function add(x, y){
return x + y
}
let operations = [sub, mult, div, add]
function logDecorator(fn){
return function decor(...args){
const result = fn(...args)
console.log(`${fn.name}(${args.join(', ')}) -> ${result}`)
return result
}
}
[sub, mult, div, add] = operations.map(logDecorator)
sub(mult(2, 5), add(2, div(12, 3)))
// mult(2, 5) -> 10
// div(12, 3) -> 4
// add(2, 4) -> 6
// sub(10, 6) -> 4
// 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment