[英]JS recursive function
I defined a function let sum = a => b => b?(a+b):a;
我定义了一个 function let sum = a => b => b?(a+b):a;
If I do console.log(sum(1)(2)());
如果我这样做console.log(sum(1)(2)());
I get the output 3
我得到 output 3
But If I try console.log(sum(1)(2)(3)());
但是如果我尝试console.log(sum(1)(2)(3)());
I get an error.我得到一个错误。
Why is that?这是为什么?
When you do当你这样做
console.log(sum(1)(2)(3));
it will throw an error because you are trying to invoke the 3rd function;它将引发错误,因为您正在尝试调用第三个 function; which you never created ( sum(1)(2)
returns 3, which isn't a function).您从未创建过的( sum(1)(2)
返回 3,这不是函数)。
Your function is a higher order function of two order.你的 function 是二阶的高阶 function。 A function that returns another function.一个 function 返回另一个 function。 So the function call should be twice only;所以 function 调用应该只有两次; you mustn't call the 3rd function, as you did above.你不能像上面那样调用第三个 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.