如何理解返回函数的函数?

发布于 2024-12-09 02:32:56 字数 252 浏览 3 评论 0原文

这是我正在努力解决的示例代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情域 2024-12-16 02:32:56

在该行中:

var greaterThanTen = greaterThan(10);

您将变量 x 赋给值 10,然后将该函数存储在 GreaterThanTen 变量中以供稍后调用。这意味着:

greaterThanTen = function(y) {
    return y > 10;
};

所以当你这样做时:

greaterThanTen(9);  #y = 9

你正在打电话:

return 9 > 10;

In the line:

var greaterThanTen = greaterThan(10);

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:

greaterThanTen = function(y) {
    return y > 10;
};

So when you do:

greaterThanTen(9);  #y = 9

You are calling:

return 9 > 10;
再浓的妆也掩不了殇 2024-12-16 02:32:56

此函数不会调用函数,而是返回一个函数。

此代码创建一个新的一元函数,其中原始二元(大于)运算符的右侧操作数预绑定到特定值。

lambda 演算中,此绑定称为 柯里化

在 Javascript 中,发生绑定是因为 greaterThan 中参数 x 提供的值永久保留在返回的内部函数(或“闭包”)的范围内。

因此,当您调用时:

var greaterThanTen = greaterThan(10);

您现在拥有的是一个函数(名为greaterThanTen),它总是将其单个参数与绑定值进行比较10.

因此:

greaterThanTen(9);

将返回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 in greaterThan is permanently retained in the scope of the inner function (or "closure") that is returned.

So, when you call:

var greaterThanTen = greaterThan(10);

what you now have is a new function (named greaterThanTen) which always compares its single parameter to the bound value of 10.

Hence:

greaterThanTen(9);

will return false.

半世晨晓 2024-12-16 02:32:56
  • 创建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,这是错误的。

  • Create greaterThan(10)
  • Create function:
    function(y){return y > x}
  • return function.

So, when you call greaterThan(10), the function returns a function whose local variable x is set to 10.

var greaterThanTen = greaterThan(10) equals:
var greaterThanTen = function(y){return y > 10};

To finish, greaterThanTen(9) is called, which equals 9 > 10, which is false.

始于初秋 2024-12-16 02:32:56

greaterThan 唯一做的就是为 x 设置一个值

函数(y) {返回(y>x);}

并将结果函数存储在变量名中,在本例中为 greaterThanTen,现在包含内容

函数(y) {返回(y>10);}

调用 greaterThanTen(9) 与查看相同

函数(y = 9) {return (y>10);}

与以下相同

函数(y = 9) {return (9>10);}

这是错误的。因此返回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

function(y) {return (y>x);}

and store the resulting function in a variable name, in this case greaterThanTen, now with the contents

function(y) {return (y>10);}

Calling greaterThanTen(9) is the same as looking at

function(y = 9) {return (y>10);}

which is the same as

function(y = 9) {return (9>10);}

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

黎歌 2024-12-16 02:32:56

greaterThanTen 变量表示一个函数,该函数接受一个参数并返回一个布尔值,无论该参数是否大于 10。

The greaterThanTen variable represents a function taking one argument and returning a boolean value whether this argument is greater then 10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文