JavaScript for-loop alternative: repeat(n, function(i) { … });

前端 未结 5 889
臣服心动
臣服心动 2021-02-09 22:05

This is the regular for-loop:

for (var i = 0; i < n; i++) { ... }

It is used to iterate over arrays, but also to just repeat some process

相关标签:
5条回答
  • 2021-02-09 22:18

    Besides what you have already stated the main downside I see is that a "return" statement will work differently. (Which is often why I end up using "for" over "$.each" many times in my own ventures.)

    0 讨论(0)
  • 2021-02-09 22:20

    It seems pretty valid. I honestly don't think that performance would decrease too much. But there is however one big downside, that is easily fixable: the break statement.

    function repeat(n, f) {
       for (var i = 0; i < n; i++) {
          var tcall=i;
          tcall.die=function(){i=n}
          f.call(tcall);
       }
    }  
    

    This way you would be able to call this.die() instead of break; which I think would throw an error.

    0 讨论(0)
  • 2021-02-09 22:36

    To address the issue of not having the break statement as others have mentioned, I would solve it this way:

    function repeat(n, f) {
        for (var i = 0; i < n; i++) {
            if (f(i) === false) return;
        }
    }
    

    Then returning false from within a loop handler will be equivalent to break.

    Another disadvantage is that the context changes. You may want to add the option of proxying a context into the loop handlers:

    function repeat(context, n, f) {
        if (!f) f = n, f = context, context = window;
    
        for (var i = 0; i < n; i++) {
            if (f.call(context, i) === false) return;
        }
    }
    

    Now, an advantage is that the index is preserved by the function scope, to avoid a common bug:

    for (var i = 0; i < 10; i++) {
        setTimeout(function () {
            alert(i); // Will alert "10" every time
        }, 1000);
    }
    
    repeat(10, function (i) {
        setTimeout(function() {
            alert(i); // Will alert "0", "1", "2", ...
        }, 1000);
    });
    
    0 讨论(0)
  • 2021-02-09 22:38

    You say you want a revolution... Well, you know: ruby did it just before (?)

    Number.prototype.times = function(func) { 
        for(var i = 0; i < Number(this); i++) {
            func(i); 
        }
    }
    

    means

    (50).times(function(i) {
        console.log(i)
    })
    

    Anyway, don't fight against C, you'll always lose :-P

    0 讨论(0)
  • 2021-02-09 22:39

    it's an interesting thought, but if you dislike the syntax for the loop, you could always do a different type of loop:

    var i = arr.length; 
    while (i--) {
        // do stuff
    }
    

    the reverse while loop is generally faster than a for loop as well.

    0 讨论(0)
提交回复
热议问题