Skip to content

Instantly share code, notes, and snippets.

@mingzhi22
Created April 14, 2016 04:15
Show Gist options
  • Save mingzhi22/854ed64d374a7b42bc351c3994ebf212 to your computer and use it in GitHub Desktop.
Save mingzhi22/854ed64d374a7b42bc351c3994ebf212 to your computer and use it in GitHub Desktop.
柯里化函数
function curry(func) {
var paramLength = func.length,
args = [];
return function collectParams () {
args = args.concat([].slice.call(arguments));
if (args.length >= paramLength) {
return func.apply(null, args);
} else {
return collectParams;
}
}
}
function add(a, b, c) {
return a + b + c;
}
console.log(curry(add)(1)(2, 3));
console.log(curry(add)(1)(2)(3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment