函數柯里化???
這是什麼???
幹嘛用的???
解決什麼問題???
我是誰???
我在那???
我要幹什麼???
我想大概只有面試的時候會用到吧!!!
畢竟,平時這麼寫程序的搬運工早已經被同胞乾死了(你他特麼是在秀技嗎???)
廢話不多說 ,先擼為敬
<code>function carry(fn, args) {
// 當前傳入函數的形參個數
var fnLength = fn.length
// 第一次args必定為空
var args = args || []
return function () {
// 獲取最新的參數數組
let newArgs = [].slice.call(arguments).concat(args)
// let newArgs = Array.prototype.slice.call(arguments).concat(args)
// let newArgs = [...args, ...arguments]
// 以上三種方式都可以
// 如何目前已獲取到的參數和形參參數相同說明是最後一次調用,需要返回結果
// 否則 ····
// 再次遞歸
if (newArgs.length >= fnLength) {
return fn.apply(this, newArgs)
} else {
return carry.call(this, fn, newArgs)
}
}
}
function add(a, b, c) {
return a + b + c
}
var muti = carry(add)
console.log(muti(1, 2, 3)) // 6
console.log(muti(1, 2)(3)) // 6
console.log(muti(1)(2, 3)) // 6/<code>
想明白了 就是這麼簡單 · · · · · ·
所以回過頭來看它的作用· · · · · ·
然而並沒有什麼作用 · · · · ·
閱讀更多 憤怒的developer 的文章