如何减少“if语句”条件?[减少if语句内的条件]

Cey*_*baş 0 javascript jquery for-loop if-statement multiple-conditions

经过几天的苦思冥想,我选择问这个问题。我有if多个条件的声明:

//var current is array of arrays of integers
if((current[rot][0] + x)<blocks.length 
    && (current[rot][1] + x)<blocks.length 
    && (current[rot][2] + x)<blocks.length 
    && (current[rot][3] + x)<blocks.length
    && !$(blocks[current[rot][0]+x]).hasClass("blockLand") 
    && !$(blocks[current[rot][1]+x]).hasClass("blockLand")
    && !$(blocks[current[rot][2]+x]).hasClass("blockLand")
    && !$(blocks[current[rot][3]+x]).hasClass("blockLand"))
    {
    //something to happen here ONCE!
    }
Run Code Online (Sandbox Code Playgroud)

因为我希望一旦我认为我无法使用,内部就会发生一些事情for loop。所以我的问题是:有没有可能的方法来减少条件数量?如何?

PS:是的,我发现我可以在里面使用flag( ) 并在这个外面的另一个true/false地方做我的事情- 但我认为这并不总是有效,因为对于每个循环,标志都会不同。ifif

Lui*_*lez 5

var b = true;

for (var i = 0; i <= 3; i++) {

    // In two lines for being clear, but it's possible just in one
    b = b && (current[rot][i] + x)<blocks.length 
    b = b && !$(blocks[current[rot][i]+x]).hasClass("blockLand"); 

    // You could speed it up this way. 
    if(!b) break;
}

if (b) {
    //something to happen here ONCE!
}
Run Code Online (Sandbox Code Playgroud)