Don't make functions within a loop. - jslint error

后端 未结 1 477
后悔当初
后悔当初 2021-01-13 17:08

I am getting this jslint error

Don\'t make functions within a loop.

I can\'t change the javascript that is causing this issue -

相关标签:
1条回答
  • 2021-01-13 17:53

    No, that valildation check is not optional.

    A possible workaround:

    // simple closure scoping i to the function.
    for(var i = 0; i < 10; i++) {
        (function (index) {
             console.log(index);
         }(i));
    }
    // this works, however it's difficult to site read and not a blast to debug
    

    A solution:

    // same exact output
    function logger(index) {
        console.log(index);
    }
    
    // same output. Minus declaring all vars at the
    // top of the function and console this passes jslint.
    for(var i = 0; i < 10; i++) {
        logger(i);
    }
    
    0 讨论(0)
提交回复
热议问题