Regarding JavaScript for() loop voodoo

前端 未结 9 1417
梦如初夏
梦如初夏 2021-02-19 08:53

I was for quite some time under the impression that a for loop could exist solely in the following format:

for (INITIALIZER; STOP CONDITION         


        
9条回答
  •  清酒与你
    2021-02-19 09:37

    That statement does comply with your initial format.

    It turns out you could add more than one sentence of each using "," ( comma )

    So:

    for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    

    Could be analyzed like this:

    for (var j,                           //INITIALIZER(s)
             x,
             i = o.length;
    
         i;                               // STOP CONDITION ( i ) 
    
         j = parseInt(Math.random() * i), // INC(DEC)REMENTER
         x = o[--i],
         o[i] = o[j],
         o[j] = x);  // CODE ( ; ) 
    

    As you see, it fits completely in your initial format.

提交回复
热议问题