JavaScript 的“if”选择
这段代码代表什么?我知道这是某种 if
替代语法...
pattern.Gotoccurance.score != null ? pattern.Gotoccurance.score : '0'
这种编码需要什么?这是更高效还是只是具有相同效率的缩短版本?
What does this bit of code represent? I know it's some kind of if
alternative syntax...
pattern.Gotoccurance.score != null ? pattern.Gotoccurance.score : '0'
What's the need for this sort of coding? Is this more efficient or just a shortened version with the same efficiency?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它是 条件 运算符,它相当于这样的东西:
但我认为您发布的代码中缺少赋值语句,如下所示:
如果
pattern.Gotoccurrence.score
不为空,则将分配score
变量:A在 JavaScript 中执行此类“默认值”赋值的常见模式是使用逻辑 OR 运算符 (
||
):pattern.Gotaccance.score
的值将是仅当该值不为 falsy 时才分配给score
变量(falsy 值为false
、null
、未定义
、0
、零长度字符串或NaN
)。否则,如果它是假的,则将分配
'0'
。性能是相当的,你应该关注可读性。我尝试在非常简单的表达式上使用三元运算符,您还可以改进格式,将其分成两行以使其更具可读性:
相关问题:
It is the conditional operator, and it is equivalent to something like this:
But I think that an assignment statement is missing in the code you posted, like this:
The
score
variable will be assigned ifpattern.Gotoccurance.score
is not null:A common pattern to do this kind of 'default value' assignments in JavaScript is to use the logical OR operator (
||
) :The value of
pattern.Gotoccurance.score
will be assigned to thescore
variable only if that value is not falsy (falsy values arefalse
,null
,undefined
,0
, zero-length string orNaN
).Otherwise, if it's falsy
'0'
will be assigned.The performance will be equivalent, and you should focus on readability. I try to use the ternary operator on expressions that are very simple, and you can also improve the formatting, splitting it up in two lines to make it more readable:
Related question:
这是一个三元运算符,一种执行 if 语句的简便方法。
如果重写的话,它看起来像这样:
This is a ternary operator, a shorthanded way to do if statements.
If re-written, it would look like this:
它称为三元运算符。
It's called the ternary operator.
至于这种编码的需要:
您有时可以使用三元运算符来降低复杂性:
例如,我有一个网页需要验证至少三个特定文本字段中的两个输入了值。 if/else 逻辑看起来相当丑陋,但三元运算符使它成为一个单行代码来确定填充了多少个字段:
与某些替代方案相比,此解决方案显得相当优雅且易于阅读。
As to the need for this sort of coding:
You can sometimes use the ternary operator to reduce complexity:
For example, I have a web page which needed to verify that at least two out of three specific text fields had values entered. The if/else logic looked pretty ugly, but the ternary operator made it a one-liner to determine how many fields were filled in:
This solution appears quite elegant and easy to read than some alternatives.
它是三元运算符/条件运算符。
在数学中,三元运算 是 n = 3 的 n 元运算。集合 A 上的三元运算采用 A 中任意给定的三个元素并将它们组合起来形成 A 的单个元素。
它是 if..else 的简写形式。
例如,想知道一个数是否为偶数。
使用 if..else 方法
使用三元
使用 switch
另一种变体是 switch:
现在,如果您有一个简单的 if..else 条件来执行,如所描述的那样,你可以继续使用三元。
但是,如果条件检查变得复杂,请使用 if..else 或 switch,因为三元中的可读性将会丢失。
例如,使用三元运算符很容易获得两个数字的最小值或最大值,但是在三个或更多数字中找到最大和第二大的数字就变得很笨拙,甚至不推荐。最好使用 if..else 代替。
It is the ternary operator/conditional operator.
In mathematics, a ternary operation is an n-ary operation with n = 3. A ternary operation on a set A takes any given three elements of A and combines them to form a single element of A.
It is a short hand form of if..else.
E.g., to want to find out if a number is even or not.
Using the if..else approach
Using ternary
Using switch
One more variant is switch:
Now, if you have a simple if..else condition to perform like the one described, you can go ahead with ternary.
But if the conditional checking becomes complex, go either with if..else or switch as readability will be lost in ternary.
For example, it is easy to get the minimum or maximum of two numbers using a ternary operator, but it becomes clumsy to find the largest and second largest among three or more numbers and is not even recommended. It is better to use if..else instead.