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
ibecomes0loop terminate.i--executed on before each iteration, , due--operator become0, it's falsy, , loop stop. - since
idecremented in condition part of loop,final-expressionnot needed too. way put it: sinceinot 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