短if-else javascript

短if-else javascript,javascript,ternary-operator,Javascript,Ternary Operator,我试图理解这段代码是如何工作的。我知道三元运算符是条件?选项1:option2但我不知道在这种情况下这是如何工作的 constructor(minSupport: number, minConfidence: number, debugMode: boolean) { this.minSupport = minSupport ? minSupport === 0 ? 0 : minSupport : 0.15; this.minConfidence

我试图理解这段代码是如何工作的。我知道三元运算符是
条件?选项1:option2
但我不知道在这种情况下这是如何工作的

constructor(minSupport: number, minConfidence: number, debugMode: boolean) {
            this.minSupport = minSupport ? minSupport === 0 ? 0 : minSupport : 0.15;
            this.minConfidence = minConfidence ? minConfidence === 0 ? 0 : minConfidence : 0.6;
            this.debugMode = debugMode || false;
        }

括号将更容易理解

this.minSupport =    minSupport ?    (minSupport    === 0 ? 0 : minSupport)    : 0.15;
this.minConfidence = minConfidence ? (minConfidence === 0 ? 0 : minConfidence) : 0.6;
这:

翻译成:

if (minSupport) {
  if (minSupport === 0) {
    this.minSupport = 0;
  } else {
    this.minSupport = minSupport;
  }
} else {
  this.minSupport = 0.15;
}
考虑到这个例子,其他的应该很容易计算出来。就我个人而言,我不喜欢你发布的嵌套三元表达式。一个好的
if/then
语句对于计算逻辑流要简单得多

this.minSupport = minSupport ? minSupport === 0 ? 0 : minSupport : 0.15;
它的实际功能(不包括非工作内容):


因此,基本上,如果minSupport为0或未通过(即未定义),则它将改为0.15。

以下是给定代码段的完整翻译

constructor(minSupport: number, minConfidence: number, debugMode: boolean) {
  this.minSupport = (() => {
    if (minSupport) { 
      if (minSupport === 0) {
        // Interpreter will never reach this point because if minSupport is 0, it would straight away go to return 0.15 statement.
        return 0; 
      } else {
        return minSupport;
      }
    } else {
      return 0.15;
    }
  })();

  this.minConfidence = (() => {
    if (minConfidence) { 
      if (minConfidence === 0) {
        // Interpreter will never reach this point because if minConfidence is 0, it would straight away go to return 0.6 statement.
        return 0;
      } else {
        return minConfidence;
      }
    } else {
      return 0.6;
    }
  })();

  this.debugMode = (() => {
    if (debugMode) {
      return debugMode;
    } else {
      return false;
    }
  })();
}
您共享的代码段似乎对0进行了过度检查,而在前面的if中已经对0进行了检查。您可能希望将代码重写为此

constructor(minSupport: number, minConfidence: number, debugMode: boolean) {
  if (minSupport === 0 || minSupport) {
    this.minSupport = minSupport;
  } else {
    this.minSupport 0.15;
  }

  if (minConfidence === 0 || minConfidence) {
    this.minConfidence = minConfidence;
  } else {
    this.minConfidence = 0.15;
  }

  if (debugMode) {
    this.debugMode = debugMode;
  } else {
    this.debugMode = false;
  }
}

这是一个案例研究,说明为什么尽可能简洁地编写代码并不能使它变得更好

把每一件事都放在一行中会使阅读和理解流程变得困难。我发现添加一点格式可以让事情更清晰:

this.minSupport =
  minSupport
    ? minSupport === 0
      ? 0
      : minSupport
    : 0.15;
有多种方法可以格式化代码,使其更易于理解。在这一点上,我们可以浏览逻辑:

this.minSupport =
  minSupport
    ? minSupport
    : minSupport === 0
      ? minSupport
      : 0.15;
如果
minSupport
是真实的:

检查
minSupport
是否为零(实际上不可能发生,因为
0
不真实)。如果是(无法),则将
此.min支持设置为
0
。否则,将
this.minSupport
设置为包含的任何值
minSupport

否则,如果
minSupport
为假:

this.min支持设置为
0.15

因此,在对该逻辑进行分析后,很明显存在一个二次检查,旨在保留
0
的值。代码有缺陷,修复方法是更改逻辑:

this.minSupport =
  minSupport
    ? minSupport
    : minSupport === 0
      ? minSupport
      : 0.15;
现在通过回流,我们可以查看逻辑,并看到它可以被压缩。如果
minSupport
是真实的,或者
minSupport
0
的话,我们想将
this.minSupport
设置为
minSupport

简化如下:

this.minSupport =
  minSupport || minSupport === 0
    ? minSupport
    : 0.15;

这是谁写的??双重检查falsy没有意义。它是嵌套的三元结构,它真的会从一些括号中受益(至少)
minSupport==0?0:minSupport
毫无意义(除了关心
-0
时,但应在注释中注明)@ZZBOV 0是错误的,因此它将在第一个三元时退出,不会进入检查0的嵌套三元?@DarrenYoung可以,但不应该。这种错误我已经见过(也犯过)很多次了。在我看来,保留显式
0
,同时覆盖隐式默认值(e.x.
未定义的
)的意图。您是否只在空格中加了空格?和括号,通过这种方式,它更清晰,尽管它绝对应该包括一个解释,这是一个巨大的逻辑:)三元表达式实际上要简单得多,因为只有一个赋值给
。minSupport
-你可以很容易地看到,条件流只影响值,而不影响它发生了什么。我说“亲自“:)哦,谢谢,伙计,我认为这是不允许的,这就是为什么我开始认为这意味着不同的东西。现在我明白了。好吧,那个代码确实有效,只是比要求的要详细得多。@andy在我看来:work===做它应该做的事to@andy因此,随着逻辑的消化,很明显,有一个二次检查打算保留0的值。代码有缺陷,修复方法是更改逻辑,可能更接近预期的含义:
this.minSupport=isNaN(minSupport)?0.15:Number(minSupport)
this.minSupport=(minSupport的类型=“Number”&&&!isNaN(minSupport))?minSupport:0.15
-了解参数类型时省略部分(如OP中的参数类型注释所示)
this.minSupport =
  minSupport || minSupport === 0
    ? minSupport
    : 0.15;