请解释此Javascript关闭练习 [英] Please explain this Javascript closure exercise

查看:91
本文介绍了请解释此Javascript关闭练习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在,我知道结果是122.任何人都可以引导我通过这个

  var hidden = mystery(3) ; 
var jumble = mystery3(hidden);
var result = jumble(2);

function mystery(input){
var secret = 4;
input + = 2;
function mystery2(multiplier){
multiplier * = input;
return secret * multiplier;
}
return mystery2;
}
function mystery3(param){
function mystery4(bonus){
return param(6)+ bonus;
}
return mystery4;
}


解决方案

必须知道函数调用和函数引用之间的区别是什么。



假设你知道这些东西,让我们来解释一下。



所以你首先有一个变量 hidden ,它被赋值为 mystery(3)。所以马上看看函数 mystery ,看看它返回什么。它会向内部函数 mystery2 返回引用。因此, hidden 包含参考,表示它没有实际的数值。下面你有一个第二个变量声明
var jumble = mystery3(hidden);
。现在为了知道 jumble 是否需要查看函数 mystery3 和它返回的值。它再次返回对内部函数 mystery4 的引用。所以现在这两个变量包含对 mystery mystery3 的内部函数的引用。



现在让我们来看看 var result = jumble(2)。执行 jumble(2)是对 jumble 保存引用的函数的实际函数调用, c $ c> mystery4 。当 mystery4 运行时,您会看到它需要一个参数 bonus ,这将是 2 var result = jumble(2)行给出。它返回 param(6)+ bonus bonus 2 ,确定,但是 param(6)?这是给 jumble hidden 赋值 mystery2 ,记得吗?因此,运行 param(6)将使用参数 6 mystery2 c>



因此,跟踪函数可能会导致一些混乱,但让我们跟随它与实际值,使它更清晰(如果这是甚至一个字)。



执行 var result = jumble(2)会给我们一个返回值 param(6)+ 2 以获取 param(6)我们进入 mystery2 with multiplier = 6 ,其中我们设置 multiplier = 6 * input 。我们的输入等于 3 + 2 = 5 ,因此乘数变为 6 * 5 = 30 作为返回值,我们乘以 4 这是我们的 var secret 。在执行 mystery2 结束时,我们有一个值 120 ,它返回到我们的 param(6) mystery4 中。如果你还记得我们的奖金 2 120 + 2 = 122 Voila!



我得到的感觉,我没有做一个很好的工作,解释这简单,但这可能是我能做的最好的。希望有帮助!


I'm a javascript noob trying to wrap my head around the closure exercise below.

Now, I know the result is 122. Can anyone walk me through this step by step (what gets passed to what), so I can understand how closures work?

var hidden = mystery(3);
var jumble = mystery3(hidden);
var result = jumble(2);

function mystery ( input ){
  var secret = 4;
  input+=2;
  function mystery2 ( multiplier ) { 
    multiplier *= input;
    return secret * multiplier;
  }
  return mystery2;
}
function mystery3 ( param ){
  function mystery4 ( bonus ){
    return param(6) + bonus;
  }
  return mystery4;
}

解决方案

In order to understand this you must know what is the difference between a function call and a reference to a function. As well as how scopes work in javascript.

Assuming you do know these things, let's get explaining.

So you first have a variable hidden that is being assigned a value of mystery(3). So immediately look at the function mystery and see what it returns. it returns a reference to an inner function mystery2. So now hidden holds a reference, meaning that it has no actual numeric value. Following you have a second variable declaration var jumble = mystery3(hidden);. Now in order to know what jumble holds you need to look at the function mystery3 and the value it returns. It, again, returns a reference to an inner function mystery4. So now the two variables you have contain references to inner functions of the closures mystery and mystery3.

Now let's have a look at var result = jumble(2). Executing jumble(2) is an actual function call to the function that jumble holds a reference to, which is mystery4. When mystery4 runs you see it requires a parameter bonus, which will be 2 given from the line var result = jumble(2). It returns param(6) + bonus. bonus is 2, ok, but what is param(6)? That is the value given to jumble: hidden, which was a reference to mystery2, remember? So running param(6) will execute mystery2 with a parameter 6

And so, tracing back the functions may have turned out a little confusing, but let's follow that with actual values to make it a little clearer ( if that's even a word ).

Executing var result = jumble(2) will give us a return value of param(6) + 2 to get param(6) we go into mystery2 with multiplier = 6, where we set multiplier = 6 * input. Our input is equal to 3+2=5, so multiplier becomes 6*5=30 and as a return value we multiply that by 4 which is our var secret. By the end of the execution of mystery2 we have a value of 120, which is returned to our param(6) in mystery4. And if you remember that our bonus was 2, 120+2=122 Voila!

I get the feeling I didn't do a very good job at explaining this simply, but that's probably the best I could do. Hope that helped!

这篇关于请解释此Javascript关闭练习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆