We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
一个考函数式编程(柯里化函数)的问题。
function currying(fn, length) { length = length || fn.length; // 注解 1 return function (...args) { // 注解 2 return args.length >= length // 注解 3 ? fn.apply(this, args) // 注解 4 : currying(fn.bind(this, ...args), length - args.length) // 注解 5 } }
const currying = fn => judge = (...args) => args.length >= fn.length ? fn(...args) : (...arg) =judge(...args, ...arg)
使用了一个闭包完成了这个效果
function add(...num) { let res = 0 //第一次调用函数时生成一个闭包来存储结果 num.forEach(item => res += item) //遍历输入参数加到res上 let ret = function (...num) { num.forEach(item => res += item) return ret } ret.toString = function () { return res } ret.valueOf = function () { return res } return ret } console.log(add(1)); // 1 console.log(add(1)(2)); // 2 console.log(add(1)(2)(3)); // 6 console.log(add(1)(2)(3,7)(4,5,6));// 28
👇~~~~ 欢迎在下方评论补充你的答案,一起来学习~:pushpin:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
📚在线阅读:前端人员需要了解的传值机制 - No.76
一个考函数式编程(柯里化函数)的问题。
解题1:
解题2:
注解说明:
解题3:
使用了一个闭包完成了这个效果
解题4 ?:
👇~~~~ 欢迎在下方评论补充你的答案,一起来学习~:pushpin:
更多内容
The text was updated successfully, but these errors were encountered: