Created
January 29, 2023 12:30
-
-
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.
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
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