JavaScript 中的问号和冒号 [英] Question mark and colon in JavaScript

查看:22
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆