文章博客

技术团队文档示例


实现compose

<h4>题目描述</h4> <p>实现函数compose,compose接受多个函数作为参数,并返回一个新的函数,新的函数会从右向左依次执行原函数, 并且上一次结果的返回值将会作为下一个函数的参数。</p> <pre><code>//实现compose函数 从右向左执行这个函数fns function compose(...fns){ return (...args) =&gt; fns.reduceRight((acc,cur) =&gt; cur(acc),...args) //第一次调用的累加器 } function a(msg) { return msg + "a"; } function b(msg) { return msg + "b"; } function c(msg) { return msg + "c"; } const f = compose( a, b, c ); console.log(f("hello"));</code></pre> <h4>reduceRight</h4> <hr /> <pre><code>arr.reduceRight(callback(accumulator, currentValue[, index[, array]])[, initialValue])</code></pre> <p>accumulator:上一次调用回调的返回值 currentValue:当前被处理的值 index:数组中的索引 array:调用 reduceRight() 的数组 initialValue:值用作回调的第一次调用的累加器。</p> <p>更多查看MDN <a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRight">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRight</a></p>

页面列表

ITEM_HTML