使用arguments和arguments.callee
function myCurrying(fn) { const vars = []; if (arguments.length > 0) { // vars.concat(Array.from(arguments)); vars = [...vars, ...arguments]; return arguments.callee; } else { return fn.apply(null, vars); } }
其餘思路
- 1.手寫jsonp
jsonp(url, data, callback) { }
- 2.通用柯里化函數
currying(func) { // return function: func(a1, a2, a3, ...) -> func_c(a1)(a2)(a3)... }
- 2.手寫柯里化函數 -柯里化在工程中有什麼用處?
https://www.zhihu.com/answer/192978122segmentfault
- 詳解JS函數的柯里化:https://www.jianshu.com/p/2975c25e4d71
柯里化實現與問題:app
// (1) sum(a1)...(an)() var currying = function(func) { const args = []; return function result(...rest) { if (rest.length === 0) { return func(...args) } args.push(...rest) return result } } // (2) sum(a1)...(an) ? // (3) new_func = curry(func); new_func(x1)(x2)(),再new_func(y1)(y2)(y3)(),怎樣保證兩次互不影響 var currying = function(func) { let args = []; return function result(...rest) { if (rest.length === 0) { ans = func(...args) args = []; return ans; } args.push(...rest) return result } }