欣迪
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)

2. Array.reduce 陣列不可為空,當 array.length === 1 時,會直接回傳 array[0] 做為結果

訂閱 IT-Monk

訂閱最新文章的發布消息! 😚😚😚
Loading

作者介紹 - 欣迪

欣迪

從設計到寫程式,發現自己有追求前端技巧的自虐傾向。不斷的踩坑,再從坑裡爬出來,慢慢對攀岩有點心得。 目前在多間公司擔任網站設計顧問。 同時也是網站架設公司負責人。