闭包语法 [英] Syntax of Closures

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

问题描述

function makeIncreaseByFunction(increaseByAmount) {
  return function (numberToIncrease) {
    return numberToIncrease + increaseByAmount;
  };
}

makeIncreaseByFunction(3)(10);

更新为Clarity
有人可以解释为什么(3 )(10)写的方式是什么?我知道10是作为内部函数的参数提供的,但为什么在语法上注明这样?如果我们有三个嵌套函数,那么参数总是按顺序写成(3)(10)(20)吗?

Updated for Clarity Can somebody explain why the (3)(10) is written the way it is? I understand that 10 is being supplied as an argument to the inner function, but why is syntactically notated like this? If we had three nested functions, would the arguments always be written in order as in (3)(10)(20)?

推荐答案

使用中间变量:

var increaseBy3 = makeIncreaseByFunction(3);
var foo = increaseBy3(10);

没有中间变量:

var foo = makeIncreaseByFunction(3)(10);

在这两种情况下,第一次调用都会传递参数 3 makeIncreaseByFunction ,结果它返回已关闭 increaseByAmount 的内部函数,其值为 3 。无论是为 makeIncreaseByFunction 返回的中间函数创建变量,还是直接调用它,它都会做同样的事情。

In both cases, the first invokation passes the argument 3 to makeIncreaseByFunction, and as a result it returns the inner function that has closed over increaseByAmount with the value of 3. Whether you create a variable for the intermediate function returned by makeIncreaseByFunction, or just invoke it directly, it does the same thing.


你能解释一下var foo = makeIncreaseByFunction(3)(10)中的更多细节吗? 10正在进入内在的功能?它只是在语法上与通常在Javascript中传递给我的参数不同。 - ggg

Can you explain a little bit more detail about how in var foo = makeIncreaseByFunction(3)(10); the 10 is getting to the inner function? It just looks syntactically different from how arguments usually get passed in Javascript to me. – ggg

makeIncreaseByFunction(3)返回一个函数,特别是inner函数在 makeIncreaseByFunction 中定义。与所有函数一样,您可以使用函数(参数)语法调用它。你可以这样写它,如果它对你更有意义:

makeIncreaseByFunction(3) returns a function, specifically the "inner function" defined inside makeIncreaseByFunction. As will all functions, you call it with the function ( arguments ) syntax. You can write it like this if it makes more sense to you this way:

( makeIncreaseByFunction(3) )(10)

这里发生的是 makeIncreaseByFunction(3)获取首先调用并返回«inner函数,然后我们调用«inner function(10)

What happens here is makeIncreaseByFunction(3) gets called first and returns the ⟪inner function⟫, and then we call ⟪inner function⟫(10).

如果你手工评估这个(我认为这就是你在语法上的意思),你可以想到它会一步一步地发生:

If you were evaluating this by hand (I think this is what you meant by "syntactically"), you could think of it happening step-by-step like this:

// Original invocation
var foo = makeIncreaseByFunction(3)(10);

// Substitute the definition of makeIncreaseByFunction
var foo = (function (increaseByAmount) {
  return function (numberToIncrease) {
    return numberToIncrease + increaseByAmount;
  };
})(3)(10);


// Apply the parameter 3
var foo = (function (numberToIncrease) {
  return numberToIncrease + 3;
})(10);


// Apply the parameter 10
var foo = 10 + 3;

// Final result
var foo = 13;






注意:如果你想成为技术,我们在这里做的只有两个 Beta减少 - 但除非你有背景知识Lambda微积分可能会让你感到困惑,而不是对你有所帮助!


Note: If you want to be technical, all we're doing here is two Beta reductions—but unless you have background with the Lambda Calculus that will probably confuse you more than it will help you!

这篇关于闭包语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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