JavaScript 中的问号和冒号
我遇到了以下行
hsb.s = max != 0 ? 255 * delta / max : 0;
?
和 :
在这种情况下意味着什么?
I came across the following line
hsb.s = max != 0 ? 255 * delta / max : 0;
What do the ?
and :
mean in this context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
它称为条件运算符(即三元运算符)。
它的形式为:
condition
?value-if-true
:value-if-false
将
?
视为“then”,将:
视为“else”。你的代码相当于
It is called the Conditional Operator (which is a ternary operator).
It has the form of:
condition
?value-if-true
:value-if-false
Think of the
?
as "then" and:
as "else".Your code is equivalent to
为了清楚起见,正确加上括号,这
则返回
255*delta/max
0
if max == 0,Properly parenthesized for clarity, it is
meaning return either
255*delta/max
if max != 00
if max == 0?
是一个三元运算符。它的工作方式类似于if
与:
结合!=
表示不等于因此,这一行的长形式将是
?
is a ternary operator. It works like anif
in conjunction with the:
!=
means not equalsSo, the long form of this line would be
当用括号编写时,这可能会更清楚一点,如下所示:
它的作用是评估第一个括号中的部分。如果结果为真,则 ? 后面的部分在 : 返回之前。如果为 false,则返回 : 后面的内容。
This is probably a bit clearer when written with brackets as follows:
What it does is evaluate the part in the first brackets. If the result is true then the part after the ? and before the : is returned. If it is false, then what follows the : is returned.
?: 是
else {}
和if(){}
问题的简写条件。因此,您的代码可以与此互换:
MDN - Conditional (三元)运算符
?: is a short-hand condition for
else {}
andif(){}
problems.So your code is interchangeable to this:
MDN - Conditional (Ternary) Operator
您所指的称为 三元运算符,它本质上是一个基本的
if
条件检查,如果三元运算中的代码块有效,则可以编写该条件检查来执行操作,否则默认为后备。三元运算采用以下语法编写:
condition
其值用作条件的表达式。exprIfTrue
如果条件计算结果为 truthy 值(等于或可以转换为 true 的值)。exprIfFalse
如果条件为 falsy 则执行的表达式 (即具有可以转换为 false 的值)。示例
采用下面的给定函数,如果提供给函数的数字是偶数,则应返回字符串
Yes
,否则返回No
。解释
上述操作分解:
(num % 2 == 0)
|这是一个简单的if
语句条件,用于检查括号内的表达式是否为 true。: "No"
这是此操作中的else
子句,如果不满足条件则返回No
。What you are referring to is called a ternary operator, it is essentially a basic
if
condition check that can be written to execute an operation if the block of code within the ternary operation is valid, otherwise default to a fallback.A ternary operation is written in the following syntax:
condition
An expression whose value is used as a condition.exprIfTrue
An expression which is evaluated if the condition evaluates to a truthy value (one which equals or can be converted to true).exprIfFalse
An expression which is executed if the condition is falsy (that is, has a value which can be converted to false).Example
Take the given function below which should return the string
Yes
if the number provided to the function is even, otherwise returnNo
.Explanation
The operation above broken down:
(num % 2 == 0)
| This is a simpleif
statement condition to check if the expression within the brackets is true.? "Yes"
If the operation is true, the string literal given is automatically returned as a result of this execution.: "No"
This is theelse
clause in this operation, if the condition is not met thenNo
is returned.<代码>? : 这不是三元运算符吗?
var x= 表达式 ?真:假
? :
isn't this the ternary operator?var x= expression ? true:false
对此要小心。尽管 -1 != true 且 -1 != false,但 -1 的计算结果为 true。相信我,我已经看到它发生了。
那么
-1? "true side" : "false side"
计算结果为 "true side"
Be careful with this. A -1 evaluates to true although -1 != true and -1 != false. Trust me, I've seen it happen.
so
-1 ? "true side" : "false side"
evaluates to "true side"