[英]How does this “higher-order functions” thing works in Javascript
From the book Eloquent Javascript by Marijn Haverbeke, there is this example while introducing the concept of higher-order functions: 在Marijn Haverbeke的《 Eloquent Javascript 》一书中,有一个示例介绍了高阶函数的概念:
function greaterThan(n) {
return function(m) { return m > n; };
}
var greaterThan10 = greaterThan(10);
console.log(greaterThan10(11));
// → true
I'm not quite sure how this works... probably answering my own question, but this is how I see it: 我不太确定这是如何工作的……可能回答了我自己的问题,但这是我的看法:
First, greaterThan(n)
is called in this line, assigning its value to the greaterThan10
variable: 首先,在此行中调用greaterThan(n)
,并将其值分配给greaterThan10
变量:
var greaterThan10 = greaterThan(10);
This makes the function stored as greaterThan10
looks like: 这使得存储为greaterThan10
的函数如下所示:
function greaterThan(10) { return function(m) { return m > 10; }; }
Then, when you call greaterThan10(11)
you are calling the function above, which translates to: 然后,当您调用greaterThan10(11)
您正在调用上面的函数,该函数转换为:
function greaterThan(10) { return function(11) { return 11 > 10; }; }
Hence returning True
as the result as 11 > 10
is true indeed. 因此,将结果作为11 > 10
返回True
确实是正确的。
Could someone confirm whether I'm correct or not? 有人可以确认我是否正确吗? Also, if someone can provide further details and comments on how this higher-order functions work in JavaScript, that would be greatly appreciated. 另外,如果有人可以提供有关此高阶函数如何在JavaScript中工作的更多详细信息和评论,将不胜感激。
You're correct, from a level of understanding, but it's evaluated slightly differently. 从某种程度来说,您是正确的,但是对它的评估略有不同。
var greaterThan10 = greaterThan(10);
This line doesn't make the function stored as greaterThan10
"look like" anything - it creates a new function, passing in the variable n
to it, so that greaterThan10
becomes a function that looks like 这行代码不会使存储为greaterThan10
的函数“看起来像”任何东西-它创建了一个新函数,将变量n
传递给它,以便greaterThan10
变成了一个看起来像
var greaterThan10 = function(m) { return m > 10; };
When you call it, you are calling this function directly, not going through the original function at all anymore. 当您调用它时,您将直接调用此函数,而不再使用原始函数了。
I also got confused while reading the example but later on reached the conclusion as below: 在阅读示例时,我也感到困惑,但后来得出如下结论:
In chapter 3: functions, it has been explained how to declare a function using the arrow notation ( =>
). 在第3章:函数中,已经说明了如何使用箭头符号( =>
)声明函数。 The arrow notation comes after the list of parameters, and if there is only one parameter then the parentheses can be ommitted around the parameter list. 箭头符号位于参数列表的后面,如果只有一个参数,则可以在参数列表周围省略括号。 So, m
is a parameter and not a function name. 因此, m
是参数而不是函数名称。
So, after the first call to greaterThan(10)
, greaterThan10
essentially becomes 因此,在第一次调用greaterThan(10)
, greaterThan10
本质上成为
var greaterThan10 = function (m) { return m > 10; }
After the second call, greaterThan10(11)
, it becomes 在第二个调用greaterThan10(11)
,它变为
function (11) { return 11 > 10; }
11 > 10, which returns true. 11> 10,则返回true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.