Created
April 14, 2016 04:15
-
-
Save mingzhi22/854ed64d374a7b42bc351c3994ebf212 to your computer and use it in GitHub Desktop.
柯里化函数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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