Weird looking Javascript for loop -
i have never seen javascript loop such for( ; i-- ; )
, used in code:
uid: function (len) { var str = ''; var src = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789'; var src_len = src.length; var = len; (; i--;) { str += src.charat(this.ran_no(0, src_len - 1)); } return str; }
i understand behavior, if share insights type of loop.
this syntax of for-loop
construction:
for ([initialization]; [condition]; [final-expression])
statement
in case for (; i--;) {
:
- no variables initialized, because
var = len;
inintialized earlier, it's not needed. - condition part truthy until
i
becomes0
loop terminate.i--
executed on before each iteration, , due--
operator become0
, it's falsy, , loop stop. - since
i
decremented in condition part of loop,final-expression
not needed too. way put it: sincei
not used inside loop, not matter whether decrement before each loop iteration or after each loop iteration.
that being said, it's better avoid writing loops above, it's pretty confusing , hard read. prefer traditional for-loops notation.
Comments
Post a Comment