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

This is a regular for loop:

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

It is used to iterate over arrays, but also to repeat a process n times.

I use the above form, but it pushes me away. Header var i = 0; i < n; i++ var i = 0; i < n; i++ var i = 0; i < n; i++ is simple ugly and needs to be rewritten literally every time it is used.

I am writing this question because I came up with an alternative:

 repeat(n, function(i) { ... }); 

Here we use the repeat function, which takes two arguments:
1. number of iterations,
2. a function that is a process that is repeated.

"code-behind" would look like this:

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

(I know the performance implications of having two additional β€œlevels” in the process scope chain)

BTW, for those of you who use the jQuery library, the above functionality can be obtained from the box using the $.each method, for example:

 $.each(Array(n), function(i) { ... }); 

And what do you think? Is this repeat function a valid alternative for a native loop? What are the downsides of this alternative (besides performance - I know about that)?

Native:

 for (var i = 0; i < 10; i++) { // do stuff } 

Alternative:

 repeat(10, function(i) { // do stuff }); 
+6
javascript jquery loops
source share
5 answers

This is an interesting thought, but if you don't like the loop syntax, you can always do a different type of loop:

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

the inverse while loop is usually faster than the for loop.

+5
source share

You say you want a revolution ... Well, you know: the 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) }) 

In any case, do not fight C, you will always lose: -P

+10
source share

To solve the problem of the lack of a break statement, as others have mentioned, I would solve it like this:

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

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

Another disadvantage is that the context is changing. You can add the context proxy option to 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; } } 

Advantage now lies in the fact that the index is stored in the scope of the function in order to avoid a common error:

 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); }); 
+2
source share

This seems to be true. Honestly, I do not think that performance will decrease too much. But there is, however, one big drawback that is easily fixed: 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); } } 

That way you can call this.die() instead of break; which I think will cause an error.

+1
source share

In addition to what you have already indicated, the main drawback that I see is that the return statement will work differently. (Which is often why I end up using the "over" over the "$ .each" many times in my own projects.)

+1
source share

All Articles