Skip to content
New issue

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

简单了解下柯里化 #5

Open
YBFACC opened this issue May 31, 2020 · 0 comments
Open

简单了解下柯里化 #5

YBFACC opened this issue May 31, 2020 · 0 comments

Comments

@YBFACC
Copy link
Owner

YBFACC commented May 31, 2020

柯里化

前言

上次在手写call等文章中看到了,支持柯里化,所以简单的了解下(只做了解)

通俗解释

只传递给函数一部分参数来调用它,让它返回一个函数去处理剩下的参数。

例子

function curry(fn) {
  var args = Array.prototype.slice.call(arguments, 1)
  return function () {
    var innerArgs = Array.prototype.slice.call(arguments)
    var finalArgs = args.concat(innerArgs)
    return fn.apply(null, finalArgs)
  }
}

function add(num1, num2) {
  return num1 + num2
}

var curriedAdd = curry(add, 5)

console.log(curriedAdd(3)) //8

参考

《JavaScript高级程序设计(第3版)》

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant