Currying 是一种将具有多个参数的函数评估为具有单个参数的函数序列的技术,换句话说,当一个函数而不是一次获取所有参数时,将第一个参数作为参数并返回一个将第二个参数作为参数的新函数并返回一个新函数,该函数采用第三个函数,依此类推,直到满足所有参数为止。
a)帮助避免一次又一次地传递相同的变量。
b)在事件处理中非常有用。
语法:
function Myfunction(a) {
return (b) => {
return (c) => {
return a * b * c
}
}
}在下面的例子中,由于没有钻营 时,所有参数都在一次(体积(11,2,3))到现有的函数来计算体积通过。
<html>
<body>
<script>
function volume(length, width, height) {
return length * width * height;
}
document.write((volume(11,2,3)));
</script>
</body>
</html>66
在下面的示例中,由于使用了currying ,因此将参数逐个传递(volume(11)(2)(3)),直到最后一个函数称为last参数。
<html>
<body>
<script>
function volume(length) {
return function(width) {
return function(height) {
return height * width * length;
}
}
}
document.write(volume(11)(2)(3))
</script>
</body>
</html>66