打赏

高阶函数 实现sum(2)(3) 柯里化

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>高阶函数 实现sum(2)(3)</title>
    </head>
    <body>
        <script type="text/javascript">
            function add() {
                var args = [].slice.call(arguments);

                var fn = function() {
                    var arg_fn = [].slice.call(arguments);
                    return add.apply(null, args.concat(arg_fn));
                }

                fn.valueOf = function() {
                    return args.reduce((a, b) => a + b);
                }
                return fn;
            }
            let s = add(1)(2)(3);
            // function
            console.log(typeof s);
            // 6
            console.log(Number(s))
        </script>

    </body>
</html>

 

posted @ 2018-11-09 16:43  孟繁贵  阅读(1003)  评论(0编辑  收藏  举报
TOP