简体   繁体   English

文本中的函数示例对我来说没有意义

[英]function example in text does not make sense to me

Reading through eloquent javascript in an attempt to wrap my head around functions, I read this example code: 阅读通俗易懂的javascript试图将我的头缠在函数周围,我读了以下示例代码:

function makeAddFunction(amount) {
  function add(number) {
    return number + amount;
  }
  return add;
}

var addTwo = makeAddFunction(2);
var addFive = makeAddFunction(5);
show(addTwo(1) + addFive(1));

I get the gist of it, but having examined the code and read the accompanying text several times over a few hours, this just hasn't clicked for me: What exacly is this code doing? 我了解了要点,但是经过几个小时的检查并仔细阅读了随附的文本,这对我来说还是没有被点击:这段代码在做什么? Where does the add function acquire the number parameter? add函数在哪里获取number参数? Does it come from the show command? 它来自show命令吗? If so, how does it get passed around? 如果是这样,它如何传播? I just don't see it... 我只是看不到...

First off, I think there some code missing in your snippets because as it stands now, show would throw undefined. 首先,我认为您的代码片段中缺少一些代码,因为就目前而言,show将抛出未定义的代码。

But, there is enough here to answer the core of your questions. 但是,这里足以回答您的问题的核心。

1) var addTwo passes the value of 2 to makeAddFunction. 1)var addTwo将值2传递给makeAddFunction。 This is "amount". 这是“金额”。 addTwo gets returned an instance of add which has value of amount of 2. addTwo返回一个值为2的add实例。

2) var addFive passes the value of 5 to makeAddFunction. 2)var addFive将值5传递给makeAddFunction。 This is "amount". 这是“金额”。 addFive gets returned an instance of add which has value of amount of 5. addFive返回一个add实例,其值为5。

3) addTwo is called with a number of 1. This is added to previous config value of 2 and returns 3. 3)用数字1调用addTwo。将其添加到先前的配置值2中并返回3。

4) addFive is called with a number of 5. This is added to previous config value of 5 and returns 6. 4)使用5调用addFive。这将添加到先前的配置值5中并返回6。

5) If a show function were defined, it would add 3+6 and show 9. 5)如果定义了show函数,它将添加3 + 6并显示9。

Where does the add function acquire the number parameter? 加法函数在哪里获取number参数?

=> number is passed as an argument of add. => number作为add的参数传递。 When you write addTwo(1) , 1 will be the number parameter. 当您编写addTwo(1) ,1将是number参数。

Where does the addTwo function acquire the amount parameter? addTwo函数在哪里获取数量参数?

=> this is called a closure. =>这称为闭包。 When you run makeAddFunction(2) , the value 2 is passed as amount and gets captured by the add function. 运行makeAddFunction(2) ,值2作为数量传递,并由add函数捕获。

The function makeAddFunction takes a number ( amount ) and returns a function that adds a number passed in as a paramter to the number passed into the outer function. 函数makeAddFunction接受一个数字( amount ),并返回一个函数,该函数将作为参数传递的数字添加到传递给外部函数的数字中。

function makeAddFunction(amount) {
   function add(number) {
     return number + amount;
   }

    return add;
}

calling var addTwo = makeAddFunction(2); 调用var addTwo = makeAddFunction(2); is equivalent to writing the following function: 等效于编写以下函数:

   var addTwo = function(number) {
     return number + 2;  // this function is actually returned by makeAddFunction
   }

you can call it with a parameter addTwo(5); 您可以使用参数addTwo(5);来调用它addTwo(5);

Here's a fiddle: http://jsfiddle.net/Eh4LK/1/ press the run button to execute 这是一个小提琴: http : //jsfiddle.net/Eh4LK/1/按下运行按钮以执行

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

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