有人可以解释这个Javascript闭包的例子 [英] Can someone explain this Javascript closure example

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

问题描述

  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));

好吧,我对这个例子在developer.mozilla下闭包感到困惑。



如果有人可以解释尽可能多的细节,以便我把头关闭。



忽略控制台.log,我只是添加了看看什么值显示,从中我可以看到,x是5和y是2当你执行add5例如。



I consoled add5()看看我得到了什么,我有NaN - 我猜这是因为我没有指定一个参数,因为它想要一个,不能添加一个数字未定义。



所以混淆是在madeAdder的内部函数中的参数y。



希望有人能提供比mozilla更好的解释...我认为线索


解决方案

/ div>

  function makeAdder(x){
return function(y){
console.log(X:+ x +Y: + y);
return x + y;
};
}

makeAdder 参数 x ,并返回一个需要参数 y 的新函数。当执行这个内部函数时,外部范围的 x 将被添加到参数 y



var add5 = makeAdder(5); 将创建一个内部函数的新实例, add5 x 为此内部函数设置为 5 。当使用 add5(2)执行 add5 时,将返回 7 ,因为 x 值(外部范围 5 )将被添加到参数 2

add10 的过程相等。当不传递参数(内部函数或外部函数或两者)参数( y

c $ c>或 x 或两者)将是 undefined undefined + number undefined + undefined 返回 NaN

var add5 = makeAdder(5); 会将 add5 设为:

  function(y){
console.log(X:+ 5 +Y:+ y);
return 5 + y;
}

因为 makeAdder(5)返回其内部函数并将x设置为5.因此,当现在使用 var sum = add5(2); 执行时,此生成函数将计算并返回 5 + y 5 + 2 )。



它不是真正地设置x到 5 (而是仍然引用外部x),但我认为这是更容易理解,并不改变任何东西在这个特定的例子


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.

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.

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.

So the confusion is that argument y in madeAdder's inner function.

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.

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. When executing this inner function, the x of the outer scope will be added to the parameter y and the sum is returned.

var add5 = makeAdder(5); will create a new instance of the inner function and store it to add5. x for this inner function is set to 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.

The procedure for add10 is equaly.

Edit when not passing a parameter ( either inner or outer function or both ) the parameter ( y or x or both ) will be undefined. undefined+number or undefined+undefined returns NaN because one or more of the summand is not a number.

Edit 2: Procedure Walkthrough:

var add5 = makeAdder(5); will set add5 to:

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); this generated function will calculate and return 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

这篇关于有人可以解释这个Javascript闭包的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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