【问题标题】:Short if else javascript简短的 if else javascript
【发布时间】:2017-08-11 14:42:23
【问题描述】:

我试图了解这段代码是如何工作的。我知道三元运算符是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;
        }

【问题讨论】:

  • 这是谁写的??双重检查 falsy 没有意义。
  • 它是嵌套的三元组,它会真正受益于一些括号(至少)
  • minSupport === 0 ? 0 : minSupport 没有意义(除了关心-0,但这应该在评论中注明)
  • @zzzzBov 0 是假的,因此它将在第一个三元组退出,并且不会进入检查 0 的嵌套一个?
  • @DarrenYoung 可以,但不应该。我已经多次看到(并犯过)这种错误。在我看来,在覆盖隐式默认值(例如 undefined)的同时保留显式 0 的意图。

标签: javascript ternary-operator


【解决方案1】:

这个:

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 语句更容易计算出逻辑流程。

【讨论】:

  • 这逻辑太棒了:)
  • 我说的是“个人”:)
  • 哦,谢谢伙计,我不认为这是允许的,这就是为什么我开始认为它意味着不同的东西。现在我明白了。
【解决方案2】:

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

将所有内容集中在一行中,难以阅读和理解流程。我发现添加一点格式可以使内容更加清晰:

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

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

如果minSupport 是真的:

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

如果minSupport 是假的:

this.minSupport 设置为0.15

因此,消化了该逻辑后,很明显有一个辅助检查打算保留0 的值。代码有bug,解决方法是改逻辑:

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

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

简化如下:

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

【讨论】:

  • 可能更接近预期含义:this.minSupport = isNaN(minSupport) ? 0.15 : Number(minSupport)this.minSupport = (typeof minSupport == "number" && !isNaN(minSupport)) ? minSupport : 0.15 - 在知道参数类型时省略部分(正如 OP 中的参数类型注释所建议的那样)
【解决方案3】:
this.minSupport = minSupport ? minSupport === 0 ? 0 : minSupport : 0.15;

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

this.minSupport = minSupport || 0.15;

所以基本上,如果 minSupport 为 0 或未通过(也称为未定义),它将改为 0.15。

【讨论】:

  • 好吧,该代码确实工作,它只是比要求的要冗长得多。
  • @andy 在我看来:work === 做了它应该做的事情
  • @andy 所以在消化了这个逻辑后,很明显有一个二级检查旨在保留 0 的值。代码有问题,解决方法是更改​​逻辑我>
【解决方案4】:

这里是给定sn-p的完整翻译

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;
    }
  })();
}

您共享的代码 sn-p 似乎对 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;
  }
}

【讨论】:

    【解决方案5】:

    括号会更容易理解

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

    【讨论】:

    • 你是不是加了空格?
    • 和括号,虽然它绝对应该包含解释,但这种方式更易读
    猜你喜欢
    • 2011-05-26
    • 1970-01-01
    • 2012-04-09
    • 2013-05-18
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2020-08-10
    • 2018-07-24
    相关资源
    最近更新 更多