码迷,mamicode.com
首页 > 其他好文 > 详细

完成 compose 函数。它接受任意多个单参函数(只接受一个参数的函数)作为参数,并且返回一个函数。它的作为用:使得类似 f(g(h(a)))这样的函数调用可以简写为 compose(f, g, h)(a)。

时间:2020-04-17 21:58:51      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:参数   span   ring   一个   语句   cti   log   one   string   

function addOne(a) {
    return a + 1;
};

function multiTwo (a) {
    return a*2;
}
function divThree (a) {
    return a/3;
}
function toString (a) {
    return a + ‘‘;
}
function split(a) {
    return a.split(‘‘);
}

function compose(divThree, multiTwo, addOne, toString, split) {
    // var func_list = [divThree, multiTwo, addOne, toString, split];
    var func_list = new Array();
    console.log(‘arguments‘, arguments);
    for (var arg_index in arguments) {
        var func = arguments[arg_index];
        if (typeof func !== ‘function‘) {
            throw "参数:‘" + func + "‘不是函数"; // throw抛出异常,在throw语句后立即终止, 它后面的语句执行不到,
        }
        func_list.push(func);
    }
    return function(value) {
        func_list.map(function(func_item) {
            value = func_item(value);
        });
        return value;
    }
}
 
console.log(compose(divThree, multiTwo, addOne, toString, split)(666));  //  ["4", "4", "5"]

 

完成 compose 函数。它接受任意多个单参函数(只接受一个参数的函数)作为参数,并且返回一个函数。它的作为用:使得类似 f(g(h(a)))这样的函数调用可以简写为 compose(f, g, h)(a)。

标签:参数   span   ring   一个   语句   cti   log   one   string   

原文地址:https://www.cnblogs.com/lvsk/p/12722499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!