外部函数如何“存储”内部函数的参数? [英] How does the outer function "store" the arguments for the inner function?
问题描述
function outside(x){
函数里面(y){
return x + y;
}
返回里面;
}
fn_inside = outside(3); //返回里面(y) - 从firebug
result = fn_inside(5); //返回8
result1 = outside(3)(5); //返回8
好像3被存储在函数'里面
',并在第二次调用时添加5并返回8。
第二次调用外部(<$ c $与第一次调用不同,c> outside(3)(5))返回值(8)而不是内部函数(在
内部) p>
好像3有点存储在函数'inside'中,在第二次调用时添加它与5和返回8。
没错。每次调用在
之外时,都会在函数内创建一个 new ,该函数具有对
之外的
调用绑定到它。它关闭了这些数据。这些被称为封闭。不要让名字困扰你,关闭不复杂。
第二次调用outside(outside(3)(5))如何返回值(8)而不是内部函数(内部)不同于第一次调用?
第二次调用在
does 返回一个函数(该函数生成的函数内部的
);但是随后你使用第二对
()
立即调用该函数。
行
outside(3)(5);
...如下所示:
var f = outside(3); //创建并获取绑定到3
f(5)的`inside`函数; //调用它,传入`5`
从你的评论中:
所以你的意思是,在第一次调用
outside(3)
,返回的内部的
方法定义变成(改为?)return3 + y;
。是吗?
关闭,但不完全。 x
的值不会被烧入里面
; 在
里面有一个对创建它的上下文的引用,并且该上下文使它可以访问 x
参数。这些都不是完全相同的东西,因为我们可以看到,如果我们稍微更新一下这个例子(并且抛开数学,这只是模糊了事情):
函数outside(name){
//'inside'使用'name'参数来创建它的'outside'
函数inside(){
返回名称;
$ b $ // changer'* changes *'name'参数的值
函数makeCaps(){
name = name.toUpperCase();
}
//返回它们两个
返回{
里面:inside,
makeCaps:makeCaps
};
}
var funcs = outside(foo);
funcs.inside(); //foo,因为'inside'使用'name'创建
//通过调用'outside',并且'name'是
//当前是foo
funcs.makeCaps(); //更改相同的'名称'
funcs.inside(); //'FOO',因为'name'已被更改
code>里面和 changer
关闭 same 上下文,这是调用 outside
创建它们。
了解每个调用创建新的上下文和新函数也很关键 outside
:
var funcs1 = outside(foo);
var funcs2 = outside(separate);
funcs1.inside(); //foo
funcs2.inside(); //separate
funcs1.makeCaps();
funcs1.inside(); //FOO
funcs2.inside(); //separate
I found this example in Mozilla developers page, but can't understand the concept.
function outside(x) {
function inside(y) {
return x + y;
}
return inside;
}
fn_inside = outside(3); //returns inside(y) -- from firebug
result = fn_inside(5); // returns 8
result1 = outside(3)(5); // returns 8
It seems like 3 is somewhat stored in the function 'inside
' and during the second call adds it with 5 and returned 8.
And how does the second call to outside (outside(3)(5)
) returned value (8) instead of the inner function (inside
) unlike the first call?
It seems like 3 is somewhat stored in the function 'inside' and during the second call adds it with 5 and returned 8.
Right. Each call to outside
creates a new inside
function, and that function has the data from the call to outside
bound to it. It "closes over" that data. These are called "closures." Don't let the name bother you, closures are not complicated.
And how does the second call to outside (outside(3)(5)) returned value (8) instead of the inner function (inside) unlike the first call?
The second call to outside
does return a function (the inside
function generated by that call); but then you're calling that function immediately with the second pair of ()
.
The line
outside(3)(5);
...breaks down like this:
var f = outside(3); // Create and get the `inside` function bound to 3
f(5); // Call it, passing in `5`
From your comment:
So what you mean is, in the first call of
outside(3)
, the 'returned'inside
method definition becomes(is changed to?) return3 + y;
. is that right?
Close, but not quite. The value of x
isn't burned into inside
; inside
has a reference to the context in which it was created, and that context gives it access to the x
argument. Those aren't quite the same thing, as we can see if we update the example a bit (and ditch the math, which just obscures things):
function outside(name) {
// 'inside' uses the 'name' argument from the call to 'outside' that created it
function inside() {
return name;
}
// 'changer' *changes* the 'name' argument's value
function makeCaps() {
name = name.toUpperCase();
}
// Return both of them
return {
inside: inside,
makeCaps: makeCaps
};
}
var funcs = outside("foo");
funcs.inside(); // "foo", because 'inside' uses the 'name' created
// by the call to 'outside', and that 'name' is
// currently "foo"
funcs.makeCaps(); // Changes that same 'name'
funcs.inside(); // "FOO", because that 'name' has been changed
It's key to understand that both inside
and changer
close over the same context, which is the context of the call to outside
that created them.
It's also key to understand that a new context and new functions are created by every call to outside
:
var funcs1 = outside("foo");
var funcs2 = outside("separate");
funcs1.inside(); // "foo"
funcs2.inside(); // "separate"
funcs1.makeCaps();
funcs1.inside(); // "FOO"
funcs2.inside(); // "separate"
这篇关于外部函数如何“存储”内部函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!