JavaScript 中的问号和冒号 [英] Question mark and colon in JavaScript
本文介绍了JavaScript 中的问号和冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了以下行
hsb.s = max != 0 ? 255 * delta / max : 0;
?
和 :
在这种情况下是什么意思?
What do the ?
and :
mean in this context?
推荐答案
It is called the Conditional Operator (which is a ternary operator).
它的形式为:condition
?value-if-true
: value-if-false
将 ?
视为then",将 :
视为else".
It has the form of: condition
? value-if-true
: value-if-false
Think of the ?
as "then" and :
as "else".
你的代码相当于
if (max != 0)
hsb.s = 255 * delta / max;
else
hsb.s = 0;
这篇关于JavaScript 中的问号和冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文