JavaScript 的“if”选择

发布于 2024-08-10 04:52:09 字数 180 浏览 12 评论 0原文

这段代码代表什么?我知道这是某种 if 替代语法...

pattern.Gotoccurance.score != null ? pattern.Gotoccurance.score : '0'

这种编码需要什么?这是更高效还是只是具有相同效率的缩短版本?

What does this bit of code represent? I know it's some kind of if alternative syntax...

pattern.Gotoccurance.score != null ? pattern.Gotoccurance.score : '0'

What's the need for this sort of coding? Is this more efficient or just a shortened version with the same efficiency?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

指尖上得阳光 2024-08-17 04:52:09

它是 条件 运算符,它相当于这样的东西:

if (pattern.Gotoccurance.score != null) {
  pattern.Gotoccurance.score;
} else {
  '0';
}

但我认为您发布的代码中缺少赋值语句,如下所示:

var score = pattern.Gotoccurance.score !=null ? pattern.Gotoccurance.score : '0';

如果 pattern.Gotoccurrence.score 不为空,则将分配 score 变量:

var score;
if (pattern.Gotoccurance.score != null) {
  score = pattern.Gotoccurance.score;
} else {
  score = '0';
}

A在 JavaScript 中执行此类“默认值”赋值的常见模式是使用逻辑 OR 运算符 (||):

var score = pattern.Gotoccurance.score ||  '0';

pattern.Gotaccance.score 的值将是仅当该值不为 falsy 时才分配给 score 变量(falsy 值为 falsenull未定义0、零长度字符串或NaN)。

否则,如果它是假的,则将分配'0'

性能是相当的,你应该关注可读性。我尝试在非常简单的表达式上使用三元运算符,您还可以改进格式,将其分成两行以使其更具可读性:

var status = (age >= 18) ? "adult"
                         : "minor";

相关问题:

It is the conditional operator, and it is equivalent to something like this:

if (pattern.Gotoccurance.score != null) {
  pattern.Gotoccurance.score;
} else {
  '0';
}

But I think that an assignment statement is missing in the code you posted, like this:

var score = pattern.Gotoccurance.score !=null ? pattern.Gotoccurance.score : '0';

The score variable will be assigned if pattern.Gotoccurance.score is not null:

var score;
if (pattern.Gotoccurance.score != null) {
  score = pattern.Gotoccurance.score;
} else {
  score = '0';
}

A common pattern to do this kind of 'default value' assignments in JavaScript is to use the logical OR operator (||) :

var score = pattern.Gotoccurance.score ||  '0';

The value of pattern.Gotoccurance.score will be assigned to the score variable only if that value is not falsy (falsy values are false, null, undefined, 0, zero-length string or NaN).

Otherwise, if it's falsy '0' will be assigned.

The performance will be equivalent, and you should focus on readability. I try to use the ternary operator on expressions that are very simple, and you can also improve the formatting, splitting it up in two lines to make it more readable:

var status = (age >= 18) ? "adult"
                         : "minor";

Related question:

淡看悲欢离合 2024-08-17 04:52:09

这是一个三元运算符,一种执行 if 语句的简便方法。

如果重写的话,它看起来像这样:

if (pattern.Gotoccurance.score != null) {
   return pattern.Gotoccurance.score;
} else {
   return '0';
}

This is a ternary operator, a shorthanded way to do if statements.

If re-written, it would look like this:

if (pattern.Gotoccurance.score != null) {
   return pattern.Gotoccurance.score;
} else {
   return '0';
}
我不在是我 2024-08-17 04:52:09

它称为三元运算符。

It's called the ternary operator.

只涨不跌 2024-08-17 04:52:09

至于这种编码的需要:

您有时可以使用三元运算符来降低复杂性:

例如,我有一个网页需要验证至少三个特定文本字段中的两个输入了值。 if/else 逻辑看起来相当丑陋,但三元运算符使它成为一个单行代码来确定填充了多少个字段:

var numberFilledInFields = ( (firstName.length > 0 ? 1 : 0) +
                     (lastName.length > 0 ? 1 : 0) +
                     (zipCode.length > 0 ? 1 : 0) );

if (numberFilledInFields < 2)
{
    validation = false;
    return;
}

与某些替代方案相比,此解决方案显得相当优雅且易于阅读。

As to the need for this sort of coding:

You can sometimes use the ternary operator to reduce complexity:

For example, I have a web page which needed to verify that at least two out of three specific text fields had values entered. The if/else logic looked pretty ugly, but the ternary operator made it a one-liner to determine how many fields were filled in:

var numberFilledInFields = ( (firstName.length > 0 ? 1 : 0) +
                     (lastName.length > 0 ? 1 : 0) +
                     (zipCode.length > 0 ? 1 : 0) );

if (numberFilledInFields < 2)
{
    validation = false;
    return;
}

This solution appears quite elegant and easy to read than some alternatives.

梦一生花开无言 2024-08-17 04:52:09

它是三元运算符/条件运算符。

在数学中,三元运算 是 n = 3 的 n 元运算。集合 A 上的三元运算采用 A 中任意给定的三个元素并将它们组合起来形成 A 的单个元素。

它是 if..else 的简写形式。

例如,想知道一个数是否为偶数。

使用 if..else 方法

function CheckEvenOdd()
{
    var number = 2;
    if(number % 2 == 0)
        alert("even");
    else
        alert("odd");
}

使用三元

function CheckEvenOdd()
{
    var number = 2;
    alert((number %2 == 0) ? "even" : "odd");
}

使用 switch

另一种变体是 switch:

function CheckEvenOdd()
{
    var number = 2;
    switch(number % 2)
    {
        case 0:
            alert("even");
            break;
        default:
            alert("odd");
            break;
    }
}

现在,如果您有一个简单的 if..else 条件来执行,如所描述的那样,你可以继续使用三元。

但是,如果条件检查变得复杂,请使用 if..elseswitch,因为三元中的可读性将会丢失。

例如,使用三元运算符很容易获得两个数字的最小值或最大值,但是在三个或更多数字中找到最大和第二大的数字就变得很笨拙,甚至不推荐。最好使用 if..else 代替。

It is the ternary operator/conditional operator.

In mathematics, a ternary operation is an n-ary operation with n = 3. A ternary operation on a set A takes any given three elements of A and combines them to form a single element of A.

It is a short hand form of if..else.

E.g., to want to find out if a number is even or not.

Using the if..else approach

function CheckEvenOdd()
{
    var number = 2;
    if(number % 2 == 0)
        alert("even");
    else
        alert("odd");
}

Using ternary

function CheckEvenOdd()
{
    var number = 2;
    alert((number %2 == 0) ? "even" : "odd");
}

Using switch

One more variant is switch:

function CheckEvenOdd()
{
    var number = 2;
    switch(number % 2)
    {
        case 0:
            alert("even");
            break;
        default:
            alert("odd");
            break;
    }
}

Now, if you have a simple if..else condition to perform like the one described, you can go ahead with ternary.

But if the conditional checking becomes complex, go either with if..else or switch as readability will be lost in ternary.

For example, it is easy to get the minimum or maximum of two numbers using a ternary operator, but it becomes clumsy to find the largest and second largest among three or more numbers and is not even recommended. It is better to use if..else instead.

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