JS-如果是语句运算符,这是怎么编译的

JS - If statement operator, how does this even compile?

本文关键字:编译 如果 语句 运算符 JS-      更新时间:2024-06-24

我正在阅读一些javascript,并试图将其中的部分用于我在Java(android)中进行的类似构建,我遇到了一个if语句,我似乎无法理解它在javascript中是如何兼容的。

// Decrementing
if (mod < 0) {
    // Check tree rank requirements
    for (var i in state[tree])
        if (i != index)
            // Figure out tier, multiply by 4 to get req points
            if (state[tree][i] > 0 && 
                // Calculate points in this tree up to this tier, and
                // subtract one if we're removing from this portion
                masteryPointReq(tree, i) > treePoints(tree, masteryTier(tree, i)) - (masteryTier(tree, index) < masteryTier(tree, i)))
                return false;
}

更具体地说,我不理解的部分是第三个if语句。

if (state[tree][i] > 0 && 
                // Calculate points in this tree up to this tier, and
                // subtract one if we're removing from this portion
                masteryPointReq(tree, i) > treePoints(tree, masteryTier(tree, i)) - (masteryTier(tree, index) < masteryTier(tree, i)))
                return false;

"-"运算符在这方面做了什么?它如何不破坏if语句?我对Javascript的了解仅限于一点点修补,所以不多。

它的总和为一个整数

var a = 3;
var b = 4;
console.log(5 - (a > b));// a > b == false
// 5
console.log(5 - (b > a));// b > a == true
// 4

false=0,true=1因此评论道:

// subtract **one** if we're removing from this portion

第三个if语句也是如何在JavaScript中编写条件的一种可能方式。如果应该执行的代码是一条指令,则不需要大括号。但是,如果您计划从jshint或jslint传递代码,那么从某种角度来看,这不是一种很好的风格。