1. 粗暴版

  1. // 简单版
  2. function add(a) {
  3. return function (b) {
  4. return function (c) {
  5. return a + b + c;
  6. }
  7. }
  8. }
  9. console.log(add(1)(2)(3)); // 6

2. 参数长度固定

  1. var add = function (m) {
  2. var temp = function (n) {
  3. return add(m + n);
  4. }
  5. temp.toString = function () {
  6. return m;
  7. }
  8. return temp;
  9. };
  10. console.log(add(3)(4)(5)); // 12
  11. console.log(add(3)(6)(9)(25)); // 43

3. 使用push方法

  1. function currying() {
  2. let args = Array.prototype.slice(arguments)
  3. let res = function () {
  4. for (let i of arguments) { args.push(i) }
  5. return res
  6. }
  7. res.toString = function () {
  8. return args.reduce((a, b) => a + b)
  9. }
  10. return res
  11. }
  12. console.log(currying(1)(2)(3).toString());