[英]Can someone explain this Javascript closure example
function makeAdder(x) {
return function(y) {
console.log("X:" + x + " Y:" + y);
return x + y;
};
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);
console.log(add5(2));
console.log(add10(2));
Ok, I am a bit confused with this example on developer.mozilla under closures. 好的,我对developer.mozilla的闭包示例有点困惑。
If someone can explain with as much detail as possible in order for me to get my head around closures. 如果有人可以提供尽可能详细的解释,以便我着手解决封闭问题。
Ignore the console.log, I just added that to see what values are displayed and from that I can see that x is 5 and y is 2 when you execute add5 for example. 忽略console.log,我只是添加了它,以查看显示了什么值,例如,在执行add5时,我可以看到x为5,y为2。
I consoled add5() to see what I get and I got NaN - and I am guessing that is because I have not specified an argument because it wants one and can't add a number to undefined. 我安慰了add5(),看看得到了什么,我得到了NaN-我猜这是因为我没有指定一个参数,因为它想要一个并且不能将数字加到undefined中。
So the confusion is that argument y in madeAdder's inner function. 因此,混淆之处在于madeAdder内部函数中的参数y。
Hope someone can provide a much better explanation than mozilla...i think the clue is environments but I'm new to this so need help from experts. 希望有人能提供比mozilla更好的解释...我认为线索是环境,但是我对此并不陌生,因此需要专家的帮助。
Thanks 谢谢
function makeAdder(x) {
return function(y) {
console.log("X:" + x + " Y:" + y);
return x + y;
};
}
makeAdder
required one parameter called x
and returns a new function requiring the parameter y
. makeAdder
需要一个名为x
参数,并返回一个需要参数y
的新函数。 When executing this inner function, the x
of the outer scope will be added to the parameter y
and the sum is returned. 执行此内部函数时,外部范围的
x
将添加到参数y
并返回总和。
var add5 = makeAdder(5);
will create a new instance of the inner function and store it to add5
. 将创建内部函数的新实例并将其存储到
add5
。 x
for this inner function is set to 5
. 此内部函数的
x
设置为5
。 when executing add5
with add5(2)
this will return 7
, because the x
value ( outer scope 5
) will be added to the parameter 2
. 当使用
add5(2)
执行add5
,它将返回7
,因为x
值(外部作用域5
)将添加到参数2
。
The procedure for add10
is equaly. add10
的过程相等。
Edit when not passing a parameter ( either inner or outer function or both ) the parameter ( y
or x
or both ) will be undefined
. 在不传递参数(内部或外部函数或两者)时进行编辑 ,则将不
undefined
参数( y
或x
或两者)。 undefined+number
or undefined+undefined
returns NaN
because one or more of the summand is not a number. undefined+number
或undefined+undefined
返回NaN
因为一个或多个被加数不是数字。
Edit 2 : Procedure Walkthrough: 编辑2 :过程演练:
var add5 = makeAdder(5);
will set add5
to: 会将
add5
设置为:
function(y) {
console.log("X:" + 5 + " Y:" + y);
return 5 + y;
}
Because makeAdder(5)
returns its inner function and sets x to 5. So when now executed with var sum = add5(2);
因为
makeAdder(5)
返回其内部函数并将x设置为5。所以现在用var sum = add5(2);
执行时var sum = add5(2);
this generated function will calculate and return 5 + y
( 5 + 2
). 此生成的函数将计算并返回
5 + y
( 5 + 2
)。
Note: it's not really setting x to 5
( instead its still a reference to the outer x ) but i think this is easier to understand and does not change anything in this particular example 注意:并不是将x真正设置为
5
(而是仍然是对外部x的引用),但是我认为这更易于理解,并且在此特定示例中不会更改任何内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.