官术网_书友最值得收藏!

  • JavaScript:Moving to ES2015
  • Ved Antani Simon Timms Narayan Prusty
  • 231字
  • 2021-07-09 19:07:33

Loops and closures

Consider the following example of using functions inside loops:

for (var i=1; i<=5; i++) {
  setTimeout( function delay(){
    console.log( i );
  }, i*100);
}

This snippet should print 1, 2, 3, 4, and 5 on the console at an interval of 100 ms, right? Instead, it prints 6, 6, 6, 6, and 6 at an interval of 100 ms. Why is this happening? Here, we encounter a common issue with closures and looping. The i variable is being updated after the function is bound. This means that every bound function handler will always print the last value stored in i. In fact, the timeout function callbacks are running after the completion of the loop. This is such a common problem that JSLint will warn you if you try to use functions this way inside a loop.

How can we fix this behavior? We can introduce a function scope and local copy of the i variable in that scope. The following snippet shows you how we can do this:

for (var i=1; i<=5; i++) {
  (function(j){
    setTimeout( function delay(){
      console.log( j );
    }, j*100);
  })( i );
}

We pass the i variable and copy it to the j variable local to the IIFE. The introduction of an IIFE inside each iteration creates a new scope for each iteration and hence updates the local copy with the correct value.

主站蜘蛛池模板: 大洼县| 巩义市| 南汇区| 安丘市| 英超| 德钦县| 井研县| 平和县| 隆林| 南雄市| 余干县| 苏尼特右旗| 平塘县| 阳江市| 山西省| 灵台县| 顺昌县| 定西市| 宁阳县| 女性| 余干县| 株洲县| 长子县| 肥西县| 晋宁县| 嫩江县| 年辖:市辖区| 桃园县| 黑山县| 利辛县| 石狮市| 长宁区| 和硕县| SHOW| 宜君县| 洪湖市| 兴化市| 循化| 阳东县| 永寿县| 弥渡县|