如何理解返回函数的函数?
这是我正在努力解决的示例代码:
function greaterThan(x) {
return function(y) {
return y > x;
};
}
var greaterThanTen = greaterThan(10);
show(greaterThanTen(9));
有没有办法用数学术语来表达它或遵循流程之类的? 我不知道为什么 10 是 x,9 是 y。
Here's the example code I'm struggling with:
function greaterThan(x) {
return function(y) {
return y > x;
};
}
var greaterThanTen = greaterThan(10);
show(greaterThanTen(9));
Is there a way to put it in math terms or follow the flow or something? I don't know why 10 is x and 9 is y.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在该行中:
您将变量 x 赋给值 10,然后将该函数存储在 GreaterThanTen 变量中以供稍后调用。这意味着:
所以当你这样做时:
你正在打电话:
In the line:
You are assinging the variable x to the value 10 and then you store the function in the greaterThanTen Variable to be called later. this means that:
So when you do:
You are calling:
此函数不会调用函数,而是返回一个函数。
此代码创建一个新的一元函数,其中原始二元(大于)运算符的右侧操作数预绑定到特定值。
在 lambda 演算中,此绑定称为 柯里化。
在 Javascript 中,发生绑定是因为
greaterThan
中参数x
提供的值永久保留在返回的内部函数(或“闭包”)的范围内。因此,当您调用时:
您现在拥有的是一个新函数(名为
greaterThanTen
),它总是将其单个参数与绑定值进行比较10.因此:
将返回
false
。This function doesn't call a function, it returns a function.
This code is creating a new unary function where the original binary (greater than) operator's right-hand operand is prebound to a specific value.
In lambda calculus this binding is known as currying.
In Javascript the binding happens because the supplied value of the parameter
x
ingreaterThan
is permanently retained in the scope of the inner function (or "closure") that is returned.So, when you call:
what you now have is a new function (named
greaterThanTen
) which always compares its single parameter to the bound value of 10.Hence:
will return
false
.greaterThan(10)
函数(y){返回y> x}
因此,当您调用
greaterThan(10)
时,该函数会返回一个函数,其局部变量x
设置为 10。var GreaterThanTen = GreaterThan(10)
代码>等于:var GreaterThanTen = function(y){return y >; 10};
最后,调用
greaterThanTen(9)
,它等于9 > 10};
10,这是错误的。greaterThan(10)
function(y){return y > x}
So, when you call
greaterThan(10)
, the function returns a function whose local variablex
is set to 10.var greaterThanTen = greaterThan(10)
equals:var greaterThanTen = function(y){return y > 10};
To finish,
greaterThanTen(9)
is called, which equals9 > 10
, which is false.greaterThan 唯一做的就是为 x 设置一个值
并将结果函数存储在变量名中,在本例中为 greaterThanTen,现在包含内容
调用 greaterThanTen(9) 与查看相同
与以下相同
这是错误的。因此返回false。
编辑:
此处返回函数的函数示例: https://i.sstatic.net/3iKQA.jpg(x 和 y 在 y>x 中互换)
合十礼
The only thing that greaterThan does is to set a value for x in
and store the resulting function in a variable name, in this case greaterThanTen, now with the contents
Calling greaterThanTen(9) is the same as looking at
which is the same as
which is false. Hence false is returned.
Edit:
Example of function that returns a function here: https://i.sstatic.net/3iKQA.jpg (x and y is switched around in y>x)
Namaste
greaterThanTen
变量表示一个函数,该函数接受一个参数并返回一个布尔值,无论该参数是否大于 10。The
greaterThanTen
variable represents a function taking one argument and returning a boolean value whether this argument is greater then 10.