简体   繁体   English

带有两个真 (?) 条件运算符的 Javascript 三元运算符

[英]Javascript ternary operator with two true (?) conditional operators

Can somebody help to substitute ?有人可以帮忙代替吗? by THEN in the code below please?由 THEN 在下面的代码中好吗?

return e = this.checked ? i - n > 0 ? 0 : i - n < 10 ? 10 : i - n : i - n < 0 ? 10 : i - n > 10 ? 0 : 10 + i - n

I am especially confused by ?我特别困惑? followed by another ?其次是另一个? at the start of the statement return e = this.checked ? i - n > 0 ? 0 .....在语句的开头return e = this.checked ? i - n > 0 ? 0 ..... return e = this.checked ? i - n > 0 ? 0 ..... return e = this.checked ? i - n > 0 ? 0 ..... . return e = this.checked ? i - n > 0 ? 0 ..... What does that mean?这意味着什么?

Thanks谢谢

Inserting parentheses and indentation, your code is equivalent to:插入括号和缩进,你的代码相当于:

return e = this.checked
? (
  i - n > 0
  ? 0
  : (
    i - n < 10
    ? 10
    : i - n
  )
)
: (
  i - n < 0
  ? 10
  : (
    i - n > 10
    ? 0
    : 10 + i - n
  )
);

In short, inside the first expression or the second expression, you're allowed to nest further ternary operators, because they resolve to expressions too - though, that doesn't mean it's a good idea, it's quite unreadable.简而言之,在第一个表达式或第二个表达式中,您可以嵌套更多的三元运算符,因为它们也解析为表达式 - 但是,这并不意味着这是一个好主意,它非常不可读。

Using if-else and this tool :使用 if-else 和这个工具

if (this.checked) {
  if (i - n > 0) {
    return 0
  } else {
    if (i - n < 10) {
      return 10
    } else {
      return i - n
    }
  }
} else {
  if (i - n < 0) {
    return 10
  } else {
    if (i - n > 10) {
      return 0
    } else {
      return 10 + i - n
    }
  }
}

For make it more readable you can always add parenthesis to your code as为了使其更具可读性,您始终可以将括号添加到您的代码中

return e = this.checked ? ( (i - n > 0 )? 0 : (i - n < 10 ? 10 : i - n ) ) : ( (i - n < 0) ? 10 : (i - n > 10 ? 0 : 10 + i - n) )

or divide into separate parts as @Nick Parsons answer.或分成单独的部分作为@Nick Parsons 的回答。

It might be useful if you break you ternary up into its separate parts.如果将三元分解为单独的部分,这可能会很有用。 Essentially it has this basic strucutre:本质上,它具有以下基本结构:

condition ? a : b

Here, a will be executed if the condition evaluates to true , and b will be triggered if it evaluates to false .在这里, a如果将执行condition的计算结果为true ,且b如果计算结果为将触发false

So, putting brackets around your separate ternary operators can help clear things up.因此,将括号括在单独的三元运算符周围可以帮助解决问题。

return e = this.checked ? (i - n > 0 ? 0 : (i - n < 10 ? 10 : i - n)) : (i - n < 0 ? 10 : (i - n > 10 ? 0 : 10 + i - n))

So here:所以在这里:

  • condition is this.checked condition是这样的。 this.checked

  • a is (i - n > 0 ? 0 : (i - n < 10 ? 10 : i - n)) a(i - n > 0 ? 0 : (i - n < 10 ? 10 : i - n))

  • b is (i - n < 0 ? 10 : (i - n > 10 ? 0 : 10 + i - n)) b(i - n < 0 ? 10 : (i - n > 10 ? 0 : 10 + i - n))

Or, you may find it easier to understand if you convert it into an if-statement like so:或者,如果将其转换为 if 语句,您可能会发现更容易理解,如下所示:

if (this.checked) {
  if (i - n > 0) {
    return 0
  } else {
    if (i - n < 10) {
      return 10;
    } else {
      return i - n;
    }
  }
} else {
  if (i - n < 0) {
    return 10;
  } else {
    if (i - n > 10) {
      return 0;
    } else {
      return 10 + i - n;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM