-
-
Save vsona/3c31c2def3d0eed1d088f890ffe31391 to your computer and use it in GitHub Desktop.
js redux compose
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
const f1 = () => console.log('f', 1) | |
const f2 = () => console.log('f', 2) | |
const f3 = () => console.log('f', 3) | |
const f4 = () => console.log('f', 4) | |
const f5 = () => console.log('f', 5) | |
/** | |
* 合并单参数方法 | |
*/ | |
function compose(...funcs) { | |
if (funcs.length === 0) { | |
return arg => arg | |
} | |
if (funcs.length === 1) { | |
return funcs[0] | |
} | |
return funcs.reduce((a, b) => (...args) => a(b(...args))) | |
} | |
let c = compose(f1, f2, f3, f4, f5) | |
c() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment