JavaScript 中的问号和冒号

发布于 2024-08-12 03:43:39 字数 135 浏览 10 评论 0原文

我遇到了以下行

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浮世清欢 2024-08-19 03:43:39

它称为条件运算符(即三元运算符)。

它的形式为: condition ? value-if-true : value-if-false
? 视为“then”,将 : 视为“else”。

你的代码相当于

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

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

if (max != 0)
  hsb.s = 255 * delta / max;
else
  hsb.s = 0;
勿挽旧人 2024-08-19 03:43:39

为了清楚起见,正确加上括号,这

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

则返回255*delta/max

  • 意味着如果 max != 0
  • 0 if max == 0,

Properly parenthesized for clarity, it is

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

meaning return either

  • 255*delta/max if max != 0
  • 0 if max == 0
嘴硬脾气大 2024-08-19 03:43:39
hsb.s = max != 0 ? 255 * delta / max : 0;

? 是一个三元运算符。它的工作方式类似于 if: 结合

!= 表示不等于

因此,这一行的长形式将是

if (max != 0) { //if max is not zero
  hsb.s = 255 * delta / max;
} else {
  hsb.s = 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;
}
枯寂 2024-08-19 03:43:39

当用括号编写时,这可能会更清楚一点,如下所示:

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

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

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.

霊感 2024-08-19 03:43:39

?: 是 else {}if(){} 问题的简写条件。
因此,您的代码可以与此互换:

if(max != 0){
       hsb.s = 225 * delta / max
}
else {
       hsb.s = 0
}

MDN - Conditional (三元)运算符

?: is a short-hand condition for else {} and if(){} problems.
So your code is interchangeable to this:

if(max != 0){
       hsb.s = 225 * delta / max
}
else {
       hsb.s = 0
}

MDN - Conditional (Ternary) Operator

为人所爱 2024-08-19 03:43:39

您所指的称为 三元运算符,它本质上是一个基本的 if 条件检查,如果三元运算中的代码块有效,则可以编写该条件检查来执行操作,否则默认为后备。

三元运算采用以下语法编写:

condition ? exprIfTrue : exprIfFalse
  • condition 其值用作条件的表达式。
  • exprIfTrue 如果条件计算结果为 truthy 值(等于或可以转换为 true 的值)。
  • exprIfFalse 如果条件为 falsy 则执行的表达式 (即具有可以转换为 false 的值)。

示例

采用下面的给定函数,如果提供给函数的数字是偶数,则应返回字符串 Yes,否则返回 No

function isEven(num) {
    return (num % 2 == 0) ? "Yes" : "No";
}

console.log("2: " + isEven(2));
console.log("3: " + isEven(3));

解释

上述操作分解:

  • (num % 2 == 0) |这是一个简单的 if 语句条件,用于检查括号内的表达式是否为 true。
  • <代码>? "Yes" 如果操作为 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 ? exprIfTrue : exprIfFalse
  • 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 return No.

function isEven(num) {
    return (num % 2 == 0) ? "Yes" : "No";
}

console.log("2: " + isEven(2));
console.log("3: " + isEven(3));

Explanation

The operation above broken down:

  • (num % 2 == 0) | This is a simple if 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 the else clause in this operation, if the condition is not met then No is returned.
誰ツ都不明白 2024-08-19 03:43:39

<代码>? : 这不是三元运算符吗?

var x= 表达式 ?真:假

? : isn't this the ternary operator?

var x= expression ? true:false

鲜肉鲜肉永远不皱 2024-08-19 03:43:39

对此要小心。尽管 -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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文