最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript array with callback function - Stack Overflow

programmeradmin4浏览0评论

I tried to have better understanding of JavaScript. Here is a piece of code that I read from JavaScript function closures.

var funcs = [];
// create a bunch of functions
for (var i = 0; i < 3; i++) {
   funcs.push(function() {
    console.log(i);
   })
}
// call them
for (var j = 0; j < 3; j++) {
  funcs[j]();
}

The array funcs has a push callback function. I don't why in the J loop, funcs[j]() will call this function to print the i in the console.
I have tried to understand this sequencey by adding some console messages:

var funcs = [];
console.log("start");
for (var i = 0; i < 3; i++) {
  console.log("i:" + i);
  funcs.push(function(){
    console.log(i);
  })
}

console.log("J loop");
for (var j=0; j<3; j++) {
  console.log("j:" + j);
  funcs[j]();
}

As expected, there is 3 for all three functions.
My question is: How does funcs[j]() calls the funcs.push(...) function? I understant the funcs[j] is reference the j element of the funcs array. But why having parentheses () will call the push(...) function?

I tried to have better understanding of JavaScript. Here is a piece of code that I read from JavaScript function closures.

var funcs = [];
// create a bunch of functions
for (var i = 0; i < 3; i++) {
   funcs.push(function() {
    console.log(i);
   })
}
// call them
for (var j = 0; j < 3; j++) {
  funcs[j]();
}

The array funcs has a push callback function. I don't why in the J loop, funcs[j]() will call this function to print the i in the console.
I have tried to understand this sequencey by adding some console messages:

var funcs = [];
console.log("start");
for (var i = 0; i < 3; i++) {
  console.log("i:" + i);
  funcs.push(function(){
    console.log(i);
  })
}

console.log("J loop");
for (var j=0; j<3; j++) {
  console.log("j:" + j);
  funcs[j]();
}

As expected, there is 3 for all three functions.
My question is: How does funcs[j]() calls the funcs.push(...) function? I understant the funcs[j] is reference the j element of the funcs array. But why having parentheses () will call the push(...) function?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Mar 21, 2016 at 15:38 ShaohaoShaohao 3,5117 gold badges27 silver badges45 bronze badges 4
  • 1 It doesn't call the .push() function; it calls the function that was passed to .push(), the little function with console.log(i); inside. – Pointy Commented Mar 21, 2016 at 15:40
  • @Pointy can you explain a little bit more? That's what I am confused about. Why does it call the function that was passed to .push()? It is a syntax or something else? – Shaohao Commented Mar 21, 2016 at 15:41
  • 1 .push add element at the end of the array. In first loop you pushed a function to the array. In second loop , you are invoking the function with () [] indicating index. – Darlyn Commented Mar 21, 2016 at 15:42
  • 2 Possibly separately from what confusing you, the code above also has the issue that the value of i may surprise you: stackoverflow./questions/750486/… – T.J. Crowder Commented Mar 21, 2016 at 15:44
Add a ment  | 

2 Answers 2

Reset to default 11

function() {console.log(i);} is an expression which evaluates to a value that is function that logs i.

funcs.push is a function that adds a value to an array.

Putting () after a function will call that function.

funcs.push(some_value) calls the push function and passes some_value as the value to put in the array.

funcs.push(function() {console.log(i);}) adds the function to the array.

The value of funcs[0] bees that function.

Putting () after a function will call that function.

funcs[0]() calls the function that is the first value in the array.

First, the 'i' variable are global, and ending the loop, i=3 Then, the functions inside funcs, use the variable "i", then, all functions print "3" in console.

Maybe you wanted to do this:

var funcs = [];
console.log("start");
for (var i = 0; i < 3; i++) {
  console.log("i:" + i);
  funcs.push(function(i){
    console.log(i);
  })
}

console.log("J loop");
for (var j=0; j<3; j++) {
  console.log("j:" + j);
  funcs[j](j);
}
发布评论

评论列表(0)

  1. 暂无评论