简体   繁体   English

Javascript,对嵌套函数的调用

[英]Javascript, the call to a nested function

I'm just come to javascript. 我只是来javascript。 I come across a code like this. 我遇到过这样的代码。

function makeAdder(a) {
   return function(b) {
       return a + b;
   };
}
x = makeAdder(5);

Here the value of x(6) is 11. It seems x here is in a "uncomplete" state, waiting another argument to be finished? 这里x(6)的值是11.看来这里的x处于“未完成”状态,等待另一个参数完成? I don't know how this works. 我不知道这是怎么回事。 Can somebody explain to me? 有人可以向我解释一下吗? any reference would be appreciated. 任何参考将不胜感激。


This is a common technique in certain functional programming languages. 这是某些函数式编程语言中的常用技术。

It's straightforward to do in Javascript, since functions are first-class values, and we can supply them as parameters to other functions, store them as properties of an object or as variables, or, as in this case, return them from other functions. 它在Javascript中很简单,因为函数是一等值,我们可以将它们作为参数提供给其他函数,将它们存储为对象的属性或作为变量,或者,就像在这种情况下,从其他函数返回它们。

This is called a higher order function, since it either accepts a function parameter or returns a function result. 这称为高阶函数,因为它接受函数参数或返回函数结果。

Values in Javascript have either global or function scope. Javascript中的值具有全局或函数范围。 The parameter a is available in the scope of the outer function, and since the inner function was created in that scope, it has access to the variable a . 参数a在外部函数的范围内可用,并且由于内部函数是在该范围内创建的,因此它可以访问变量a This is called a closure . 这称为闭包

A number of libraries offer a curry function that wraps up a plain function such as 许多库提供了一个curry函数,它包含了一个简单的函数,如

function f(a, b) {
  return a + b;
}

by using instead: 通过使用代替:

var g = curry(function f(a, b) {
  return a + b;
});

so that now you can call it either as 所以现在你可以把它称为

g(6, 36); //=> 42

or as 或者作为

var add6 = g(6);
add6(10);  //=> 16;

But if you always want to do this in two steps, you can define it the way your makeAdder does. 但如果您总是希望分两步完成此操作,则可以按照makeAdder的方式进行定义。

If you're interested in this style of programming, there are a number of libraries that try to help with it. 如果您对这种编程风格感兴趣,可以使用许多库来帮助它。 My personal favorite is Ramda (disclaimer: I'm a core contributor to Ramda.) 我个人最喜欢的是Ramda (免责声明:我是Ramda的核心贡献者。)

Ok, here's my interpretation. 好的,这是我的解释。 So your function makeAdder() returns another function, which will add the parameter a to b . 所以你的函数makeAdder()返回另一个函数,它将参数a添加到b When you run makeAdder(5) , you are getting back a function with the parameter a set to 5. 运行makeAdder(5) ,您将返回一个参数a设置为5的函数。

Now you have a function that adds a parameter to 5. When you call x(6) , you are getting back 5 + 6. 现在你有一个函数可以将参数添加到5.当你调用x(6) ,你会回到5 + 6。

It's a bit difficult to get your head around the whole thing, and I had to step through it for a few minutes to understand it myself. 让你的头脑绕过整个事情有点困难,我不得不单步通过它几分钟来了解它。 here's a step by step: 这是一步一步:

function makeAdder(a) {

Here, we've initialised makeAdder() as a function that takes a single parameter, a 在这里,我们将makeAdder()初始化为一个带有单个参数的函数, a

   return function(b) {

Our function will return a new function that takes a single parameter, b 我们的函数将返回一个带有单个参数的新函数, b

       return a + b;

This new function returns a plus b - a is set to whatever we passed in to the first function and doesn't change 这个新函数返回a加号b - a设置为我们传入第一个函数的任何值,并且不会改变

x = makeAdder(5);

Now we run the original function, passing in 5. 5 will trickle down into the new function (remember it won't change) and what we get back is the new function that adds that 5 to a parameter. 现在我们运行原始函数,传入5. 5将逐渐进入新函数(记住它不会改变),我们得到的是将函数5添加到参数的新函数。 x is now storing that new function. x现在正在存储该新功能。

x(6);

This is us running the new function stored in x - and we're passing in 6. The new function will add 6 to the 5 we passed in earlier, and we get back 11. 这是我们运行存储在x的新函数 - 我们传入6.新函数将在我们之前传递的5中添加6,然后我们返回11。

It's a pattern call partial application. 这是一个模式调用部分应用程序。 It allows a to be cached before b is known it allows allows a to be shared across multiple invocations: 它允许在知道b之前对其进行缓存,它允许在多个调用之间共享:

x = makeAdder(5);

a = x(6);  // 11
b = x(7);  // 12
c = x(8);  // 13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM