函数柯里化

65 阅读1分钟
function add(...args) {
            return args.reduce((a, b) => a + b)
        }

        function currying(fn) {
            let args = []
            return function _c(...newArgs) {
                if (newArgs.length) {
                    args = [
                        ...args,
                        ...newArgs
                    ]
                    return _c
                } else {
                    return fn.apply(this, args)
                }
            }
        }

        let addCurry = currying(add)
        console.log(addCurry(1)(2,3)(4)(5)());  //10