1

我正在深入研究另一个开发人员的代码,并遇到了这条疯狂的路线。看起来有很多比较正在进行,但我无法理解它。

num > -1 ? "down" === position && value >= this._offset ? this.hideFunc() : "up" === position && value >= this._offset && this.showFunc() : this.showFunc()

我怎样才能破译这个?

4

4 回答 4

7
if (num > -1) {
    if ( "down" === position && value >= this._offset ) {
       return this.hideFunc();
    } else if ("up" === position && value >= this._offset) {
       return this.showFunc();
    } else {
       return; //Doesn't exist in the one liner but the function must return something..
    }
} else {
    return this.showFunc();
}
于 2013-05-07T21:51:21.153 回答
1

有趣的是,这里的所有其他答案似乎都错过了解构三元运算符的一个潜在分支。

if (num > -1) {
    if ( "down" === position && value >= this._offset ) {
       return this.hideFunc();
    } else if ("up" === position && value >= this._offset) {
       return this.showFunc();

    /*** HERE'S THE BRANCH OTHER ANSWERS MISS ***/
    } else {
       return false;
    }

} else {
    return this.showFunc();
}

这来自您问题中可怕的三元表达式的这个特定部分:

"up" === position && value >= this._offset && this.showFunc()

我们可以将其分解为两个分支:

"up" === position && value >= this._offset

显然,这个表达式实际上也是两个分支;但我们可以肯定,无论哪种情况,结果都是truefalse如果结果是true那么我们将得到这个

this.showFunc()

否则,短路逻辑将已经导致 的结果false。因此缺少分支。

具体来说:如果出现这种情况:

  • num大于-1,并且:
  • position既不是"down"也不是"up",或者:
  • position"up“但value小于this._offset

三元表达式涵盖了这种情况,这个答案也是如此。其他答案没有(代码将一直通过并导致undefined)。

不过,公平地说:无论如何都不会使用结果(如果原始代码确实是您包含的单行代码),在这种情况下没有功能差异。但是,我觉得有必要写这个答案,主要是为了强调测试现有功能的任何重构的重要性,即使之前的代码很糟糕,即使你在分析中一直很小心。

于 2013-05-07T23:07:11.897 回答
1

运算符优先级告诉我们三元运算符是最外层的,然后是 AND 和比较。写这个的更好(更清晰)的方法是

(num > -1)
  ? ( (("down" === position) && (value >= this._offset))
      ? this.hideFunc()
      : (("up" === position) && (value >= this._offset)) && this.showFunc() )
  : this.showFunc()

(括号只是为了明确而添加的,您不妨省略它们 - 在缩进中有足够的信息)

现在您“只”需要了解AND 运算符的短路评估,以了解它是一种复杂的编写方式

if (num > -1)
    if (value >= this._offset) {
        if ("down" === position)
            this.hideFunc();
        else if ("up" === position)
            this.showFunc();
    }
else
    this.showFunc();

(并从函数调用中取回返回值或false

于 2013-05-07T22:06:09.047 回答
0

@MajidL 向您展示了它是什么,但没有展示如何破译它。

这就是所谓的三元运算符,它们可能很难理解。

所以要让它停下来,你有你的第一句话:

num > -1 

这在其他任何事情之前都会被评估,如果它是真的,它会落入第二个三元运算符,该运算符&&运算符结合:

"down" === position && value >= this._offset

由于这也是一个三元运算符,它也有一个else,但这也恰好是一个if检查:

"up" === position && value >= this._offset

所以你最终得到了这个结构:

if(num > -1){
    if("down" === position && value >= this._offset){

    }else if("up" === position && value >= this._offset){

    }  
}else{

}

这使您可以弄清楚哪些返回值与哪些检查有关。这实际上是 IMO 的棘手部分。

最后一个返回值实际上是最简单的,它总是属于else第一个三元运算符的,因为首先考虑外部运算符。所以这:

this.showFunc();

像这样进入我们的结构:

if(num > -1){
    if("down" === position && value >= this._offset){

    }else if("up" === position && value >= this._offset){

    }  
}else{
    return this.showFunc();
}

这留下了中间检查。在这里,您的其他开发人员做了一些偷偷摸摸且不太易于维护的事情,他使用外部值作为两个三元运算的返回值。我不建议这样做。

这也意味着内部值this.hideFunc()只能属于内部三元运算符,我们最终得到完整的语句。

if(num > -1){
    if("down" === position && value >= this._offset){
        return this.hideFunc();
    }else if("up" === position && value >= this._offset){
        return this.showFunc();
    }  
}else{
    return this.showFunc();
} 
于 2013-05-07T22:16:04.653 回答