简体   繁体   English

Javascript 仅用于假表达式的三元条件运算符

[英]Javascript ternary conditional operator for only false expression

I have just learned how to use Javascript ternary conditional operator.我刚刚学会了如何使用 Javascript 三元条件运算符。

condition ? true : false

The question is i want to use the operator with only false expression.问题是我想使用只有假表达式的运算符。

what i expect我期待什么

condition : false expression

but above code not work, is there any way?但是上面的代码不起作用,有什么办法吗?

Javascript ternary conditional operator for only false expression Javascript 仅用于假表达式的三元条件运算符

This is not possible.这不可能。 You don't even need to know ECMAScript or TypeScript to know that this is not possible, just basic English: the word " ternary " means "composed of three things", so a ternary conditional operator can by definition only have the condition and two consequences.你甚至不需要知道 ECMAScript 或 TypeScript 就知道这是不可能的,只需要基本的英语:“三元”这个词的意思是“由三件事组成”,所以三元条件运算符可以根据定义只有条件两个结果。

If it only had a condition and one consequence, it would be a binary conditional operator.如果它只有一个条件和一个结果,它将是一个二元条件运算符。

Please see this Ternary Operator .请参阅此三元运算符 It clearly says ternary operator has three parts.它清楚地表明三元运算符具有三个部分。

  1. Condition健康)状况
  2. Value if true如果为真值
  3. value if false假值

So you cannot do as you asked.所以你不能按照你的要求去做。 Instead you should use simple if and check if condition is false and return false value.相反,您应该使用简单的 if 并检查条件是否为假并返回假值。

condition || expression

can use like可以像这样使用

form.isValid || form.submit()

The form's submit method will not be evaluated if the isValid property is falsey.如果 isValid 属性为假,则不会评估表单的提交方法。

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

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