JavaScript中的问号和冒号

时间:2022-05-17 00:43:33

I came across the following line

我遇到了下面一行

hsb.s = max != 0 ? 255 * delta / max : 0;

What do the ? and : mean in this context?

做什么?在这种情况下是什么意思?

6 个解决方案

#1


296  

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".

它的形式是:condition ?value-if-true: value-if-false思考?如"then"和:as "else"。

Your code is equivalent to

您的代码是等效的。

if (max != 0)
  hsb.s = 255 * delta / max;
else
  hsb.s = 0;

#2


30  

Properly parenthesized for clarity, it is

为了清晰,它被适当地加上括号

hsb.s = (max != 0) ? (255 * delta / max) : 0;

meaning return either

这意味着返回

  • 255*delta/max if max != 0
  • 255*如果max != 0
  • 0 if max == 0
  • 如果max == 0。

#3


7  

hsb.s = max != 0 ? 255 * delta / max : 0;

hsb。s = max != 0 ?255 * delta / max: 0;

? is a ternary operator, it works like an if in conjunction with the :

吗?是一个三元运算符,它与:

!= means not equals

! =意味着不平等

So, the long form of this line would be

这条线的长形式是

if (max != 0) { //if max is not zero
  hsb.s = 255 * delta / max;
} else {
  hsb.s = 0;
}

#4


6  

This is probably a bit clearer when written with brackets as follows:

如果用括号括起来,这一点可能会更清楚一些:

hsb.s = (max != 0) ? (255 * delta / max) : 0;

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.

它的作用是评估第一个括号中的部分。如果结果是真的,那么后面的部分呢?在返回之前。如果为假,则返回:后面的内容。

#5


0  

? : isn't this the ternary operator?

吗?这不是三元算符吗?

var x= expression ? true:false

var x =表达式?真:假

#6


-3  

Be careful with this. A -1 evaluates to true although -1 != true and -1 != false. Trust me, I've seen it happen.

小心这一点。A -1的值为true,但-1 != true和-1 != false。相信我,我亲眼目睹了这一切。

so

所以

-1 ? "true side" : "false side"

1 ?“真面”:“假面”

evaluates to "true side"

计算结果为“真正的一面”

#1


296  

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".

它的形式是:condition ?value-if-true: value-if-false思考?如"then"和:as "else"。

Your code is equivalent to

您的代码是等效的。

if (max != 0)
  hsb.s = 255 * delta / max;
else
  hsb.s = 0;

#2


30  

Properly parenthesized for clarity, it is

为了清晰,它被适当地加上括号

hsb.s = (max != 0) ? (255 * delta / max) : 0;

meaning return either

这意味着返回

  • 255*delta/max if max != 0
  • 255*如果max != 0
  • 0 if max == 0
  • 如果max == 0。

#3


7  

hsb.s = max != 0 ? 255 * delta / max : 0;

hsb。s = max != 0 ?255 * delta / max: 0;

? is a ternary operator, it works like an if in conjunction with the :

吗?是一个三元运算符,它与:

!= means not equals

! =意味着不平等

So, the long form of this line would be

这条线的长形式是

if (max != 0) { //if max is not zero
  hsb.s = 255 * delta / max;
} else {
  hsb.s = 0;
}

#4


6  

This is probably a bit clearer when written with brackets as follows:

如果用括号括起来,这一点可能会更清楚一些:

hsb.s = (max != 0) ? (255 * delta / max) : 0;

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.

它的作用是评估第一个括号中的部分。如果结果是真的,那么后面的部分呢?在返回之前。如果为假,则返回:后面的内容。

#5


0  

? : isn't this the ternary operator?

吗?这不是三元算符吗?

var x= expression ? true:false

var x =表达式?真:假

#6


-3  

Be careful with this. A -1 evaluates to true although -1 != true and -1 != false. Trust me, I've seen it happen.

小心这一点。A -1的值为true,但-1 != true和-1 != false。相信我,我亲眼目睹了这一切。

so

所以

-1 ? "true side" : "false side"

1 ?“真面”:“假面”

evaluates to "true side"

计算结果为“真正的一面”