需要帮助理解JS代码 [英] need help understanding JS code
问题描述
您好我是javascript新手,我不明白以下代码:
var createAdders = function(){
var fns = [];
for(var i = 1; i <4; i ++){
fns [i] =(function(n){
return i + n ;
});
}
return fns;
}
var adders = createAdders();
adders [1](7); // 11?
adders [2](7); // 11?
adders [3](7); // 11?
根据我的理解,7是作为参数传递的,但是 createAdders
不会将7赋给任何变量,所以这意味着7被传递给 createAdders()
中的下一个函数,即匿名函数和
上面的代码似乎工作,但对于每个调用,结果是11.
我发现这个代码在一个非常可靠的博客作为一个例子,一个情况下,闭包将上面的代码然后改变以下面的方式来引用闭包的优点。
var createAdders = function(){
var fns = [];
for(var i = 1; i <4; i ++){
(function(i){
fns [i] =(function(n){
return i + n;
});
})(i)////为什么在这里使用(i)?它的目的是什么?////
}
return fns;
}
var adders = createAdders();
adders [1](7); // 8
adders [2](7); // 9
adders [3](7); // 10
这里是否也应用相同的逻辑? p>
我需要了解如何为n分配值7
为什么(i)在新代码的函数结尾处使用
内部函数总是能够访问它们的外部函数变量,即使在外部函数完成执行之后。这个行为称为Closure。
在第一个片段中,你迭代超过3次,并在数组中推送函数。当循环完成执行你的变量i设置为4.并且您推入数组内的函数可以访问此变量。因此,无论在什么数组的索引执行函数,你总是得到11(4 + 7)。
在第二个片段中,你使用一个自执行匿名函数保持i的值。这个匿名函数会在每次交互时立即执行。因为在这个匿名函数中定义的闭包函数每次都引用i的新值。
所以你在数组中有一个三个函数,每个都有i (1,2,3)
编写第二个代码片段如下:
var createAdders = function {
var fns = [];
for(var i = 1; i <4; i ++){
(function(a){
fns [a] =(function(n){
return a + n;
});
})(i)
}
return fns;}
在javascript中,你可以有函数作为返回值。
和第二个数组可以存储函数里面的自己。
你在代码中做一个函数在数组中接受一个参数,然后调用它。
adders [1](7);
可按如下细分:
var fn = adders [1];
fn(7); // thats how n is set to 7
Hi I am new to javascript and I cannot understand the following code:
var createAdders = function () {
var fns = [];
for (var i = 1; i < 4; i++) {
fns[i] = (function (n) {
return i + n;
});
}
return fns;
}
var adders = createAdders();
adders[1](7); //11 ??
adders[2](7); //11 ??
adders[3](7); //11 ??
From what I understand 7 is being passed as an argument but createAdders()
doesn't assign 7 to any variable so does that mean 7 is being passed to the very next function within createAdders()
i.e the anonymous function and assigned to the variable n
.
Is my logic correct?
The code above does seem to work but for every call the result is 11. I found this code in a very reliable blog as an example to a situation where closures would be useful.The above code is then altered in the following way to cite the advantages of closures.
var createAdders = function () {
var fns = [];
for (var i = 1; i < 4; i++) {
(function (i) {
fns[i] = (function (n) {
return i + n;
});
})(i) //// why is (i) used here? what purpose does it serve??////
}
return fns;
}
var adders = createAdders();
adders[1](7); //8
adders[2](7); //9
adders[3](7); //10
Is the same logic applied here as well?
I need to understand how n is being assigned the value 7
Why is (i) being used at the end of the function in the new code?
In javascript , inner function always have access to their outer function variable even after the outer function is finished executing. This behaviour is called Closure.
In first snippet, you're iterating over 3 times and pushing the function inside an array.When the loop is finished executing your variable i is set to 4. And function you pushed inside the array has access to this variable . So no matter at what index of array you execute the function you always get 11 ( 4 + 7).
In Second snippet , you are using an self executing anonymous function to hold the value of i . This anonymous function gets immediately executed with every interation . Because of closure function defined inside this anonymous function has a reference to new value of i every time.
So you have a three function inside the array each with seperate value of i (1,2,3) Writing second snippet as follows makes it more clear
var createAdders = function(){
var fns = [ ];
for (var i=1; i<4; i++) {
(function(a) {
fns[a] = (function(n) {
return a+n;
});
})(i)
}
return fns;}
In javascript you can have function as return value. and secondly arrays can store function inside themself.
What your doing in code is pushing a function inside array which accepts a single argument and later invoking it.
adders[1](7);
can be broken down as follow
var fn = adders[1];
fn(7); //thats how n is set to 7
这篇关于需要帮助理解JS代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!