函数内调用函数的函数 [英] scope calling functions inside functions
问题描述
我想了解更多关于函数和变量范围的知识,并且找到了一个相当不错的教程,但是这部分我只是没有得到。
任务: 创建一个可以像这样工作的函数和: 解决方案: 解释: 为了使 我的解释: 问题: 我无法弄清楚,在哪里需要以不同的方式思考,因为就像我上面所描述的那样,函数不会再被调用,所以代码的哪一部分会使'多个括号'可能吗? 此外,我是否正确地假设返回
sum(a)(b)= a + b
并接受任意数目的括号。例子:
pre $ code> sum(1)(2)== 3
sum(5)( - 1)(2) )== 6
函数sum(a){
var sum = a;
函数f(b){
sum + = b;
返回f;
}
f.toString = function(){return sum};
return f; //第12行
}
alert(sum(1)(2)); // 3e
sum(1)
可调用为 sum(1)(2)
,它必须返回一个函数。
可以使用 valueOf
来调用函数或将其转换为数字。
解决方案真的不言自明:
函数f(b)
中的 f
返回到范围,该行从02-12行开始。
f.toString
中的 f
,是当前返回的 f $ (b)
下一个返回f
返回到外部作用域 sum(a)
。
f
s的位置?如果有人愿意给出一些解释,那将是非常棒的。 f
。
函数 f
也返回一个函数:实际上,函数 f
返回本身。
在 sum
中定义函数 f
时,它可以永久访问当前可见的所有变量在范围链中。在这里,它包括本地定义的变量 sum
(本地运行总计计数)和 f
(函数本身) 。 (闭包就是我们称之为 f
的函数代码及其所有范围内的变量。)
<因为
f
会自行返回,您可以通过重复调用链接 f
: var this_is_f = sum(1);
var same_f_again = this_is_f(2);
var f_a_third_time = same_f_again(3);
或者简单地说:
总和(1)(2)(3);
重要的是要注意,在我的第一个示例中,我不创建新的函数;相反,我只是用三个不同的标识符来引用完全相同的函数对象。
每次调用 sum
创建一个全新的 f
与一个新的本地 sum
在其范围内(这里,我的意思是本地 sum
定义在名为 sum
的函数的第一行)。但是,调用函数 sum
不会破坏任何旧的 f
,因为每次调用 sum
实例化一个新的 f
(并且对已经创建的任何其他 f
之前调用 sum
)。这样,您可以运行多个计数器:
var first_tally = sum(1)(2); // first:3
var second tally = sum(4)(5); // second:9
first_tally(3); // first:6
second_tally(6); // second:15
随时可以看到有意义的结果的原因是 f
强化为 sum
的值,而不是显示其源代码。
Hope somebody finds the time to explain little about functions in functions and scoping. I am trying to understand little more on functions and scope of variables and found a quite good tutorial, but this part I just do not get.
The task:
Create a function sum that will work like that: sum(a)(b) = a+b
and accepts any number of brackets. Examples:
sum(1)(2) == 3
sum(5)(-1)(2) == 6
The solution:
function sum(a) {
var sum = a;
function f(b){
sum += b;
return f;
}
f.toString = function() { return sum };
return f; //line 12
}
alert( sum(1)(2) ); // 3e
The Explanation:
To make sum(1)
callable as sum(1)(2)
, it must return a function.
The function can be either called or converted to a number with valueOf
.
The solution is really self-explanatory:
My interpretation:
This f
in function f(b)
returned to the scope, which is from line 02 - 12.
The f
in f.toString
, is the currently returned f
from function(b)
The next return f
returns to the scope which is outside the function sum(a)
.
Problem:
I cannot figure out, where I need to think differently, because like I described above, the function would not be called again, so where is the part of the code, that make the 'several parentheses' possible?
Moreover, did I correctly assume where the f
s are returned? Would be great if somebody would give some explanations.
The function sum
returns a function, which we refer to as f
.
The function f
also returns a function: in fact, the function f
returns itself.
When the function f
is defined inside of sum
, it gets permanent access to all variables currently visible in the scope chain. Here, it includes the locally-defined variable sum
(the local running sum tally) and f
(the function itself). (A "closure" is what we call the functional code of f
along with all its in-scope variables.)
Because f
returns itself, you can chain f
with repeated calls:
var this_is_f = sum(1);
var same_f_again = this_is_f(2);
var f_a_third_time = same_f_again(3);
Or simply:
sum(1)(2)(3);
It is important to note that in my first example, I don't create new functions; instead, I just refer to the exact same function object with three different identifiers.
Each call to sum
creates a brand-new f
with a new local sum
in its scope (here, I mean the local sum
defined on the first line of the function named sum
). However, calling the function sum
does not clobber any old f
, because each call to sum
instantiates a new f
(and knows nothing about any other f
s that have been created on prior calls to sum
). That way, you can have multiple tallies running:
var first_tally = sum(1)(2); // first: 3
var second tally = sum(4)(5); // second: 9
first_tally(3); // first: 6
second_tally(6); // second: 15
The reason you're able to see a meaningful result at any time is that f
stingifies to the value of sum
, instead of showing you its source code.
这篇关于函数内调用函数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!