A for loop without any {}

后端 未结 6 1885
借酒劲吻你
借酒劲吻你 2020-12-10 18:10

So I was reading about shuffling an array. And then I came across this script:

shuffle = function(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 18:35

    First, I'd like to note that such a loop is considered bad style, since it's very unreadable and causes a lot of confusion. This is a typical example of optimization gone wrong.

    Looking at the specs, you'll find that for(...) must be followed by a statement. It can be any statement, including blocks. So all of these are valid:

    for (...)
        foo;  // expression statement
    

    ,

    for(...)
        {
           // block statement
        }
    

    ,

    for(...)
       if(...) // If statement
          foo;
    

    , and of course

    for (...)
        ;
    

    since ";" is the empty statement. It does nothing, but it's enough to make for(...); syntactically valid.

    Now, for the commas. Note that the contents of the parens must be three expressions, (each of them optional), separated by semicolons. Pretty much "everything" qualifies as expressions, including comma-separated lists of expressions. Though little known, these work basically everywhere in JS, not only in for loops. They are simply evaluated one after the other.

    So, your loop can be rewritten like so

    shuffle = function(o) {
        var j, x, i = o.length;
        while (i) { // for-loops are just while-loops in disguise
            j = parseInt(Math.random() * i), // even better: replace , by ;
            x = o[--i],
            o[i] = o[j],
            o[j] = x;
        }
        return o;
    };
    

    Also, x = o[--i] should be written as i--; x = o[i].

提交回复
热议问题