
fn1 = function (x) { x + 1 } fn2 = function (x) { x + 3 } fn3 = function (x) { x * 3 } // compact(fn1,fn2,fn3)(1) 的結果等於以下 // fn1(fn2(fn3(1))) const compact = function () { your code... }
方法一 使用遞迴
See the Pen
前端模擬試題 – function 遞迴 (不使用 reduce) by Dean (@hcd1983)
on CodePen.0
方法二 array.reduce
See the Pen
前端模擬試題 – function 遞迴 by Dean (@hcd1983)
on CodePen.0
技巧
1. arguments 是一個特殊的 Class 不能直接當成 array 使用。需要透過以下方法轉化
Array.prototype.slice.call(arguments)