简写 if else javascript

Short if else javascript

我想了解这段代码是如何工作的。我知道三元运算符是 condition ? option1 : 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;

这个:

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

翻译成:

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;

它实际做了什么(撇开不工作的东西):

this.minSupport = 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 中已经处理过了。您可能希望为此重写代码。

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;

有多种方法可以格式化该代码以使其更易于理解。此时我们可以走一遍逻辑:

如果minSupport是真实的:

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

否则如果 minSupport 是错误的:

this.minSupport设置为0.15

所以在消化了这个逻辑之后,很明显有一个二次检查 打算 保留 0 的值。代码有问题,解决方法是更改​​逻辑:

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

现在有了回流,我们可以查看逻辑并看到它可以被压缩。我们想将 this.minSupport 设置为 minSupport 如果 minSupport 为真 如果 minSupport0.

简化后的样子:

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