javascript 对速记语法感到困惑: x > 0 ? 1:-1;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10566990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:16:38  来源:igfitidea点击:

Confused by shorthand syntax: x > 0 ? 1 : -1;

javascriptsyntaxconditional-operator

提问by osami

What does the following Javascript syntax mean? Please describe the whole syntax:

以下 Javascript 语法是什么意思?请描述整个语法:

var x = 0;
x > 0 ? 1 : -1;  // confused about this line
alert(x);

回答by Madara's Ghost

That on its own means nothing. You will alert x's value, which is 0, and that's it. The second statement is meaningless unless you assign it to something. If however you would have done this:

这本身没有任何意义。您将 alertx的值为 0,仅此而已。第二个语句是没有意义的,除非你将它分配给某个东西。但是,如果您会这样做:

var x=0;
var y = x > 0 ? 1 : -1;
alert(y);

You would have gotten -1.

你会得到-1。

The Conditional Operator, is a shorthand for IF statements, it basically says:

条件运算符是 IF 语句的简写,它基本上说:

Assert if x > 0. If so, assign 1. If not, assign -1.

断言如果x > 0。如果是,则分配 1。如果不是,则分配 -1。

Or on a more general form:

或者更一般的形式:

CONDITION ? VALUE_IF_TRUE : VALUE_IF_FALSE;

Where:

在哪里:

  • CONDITION- can be anything which evaluates to a boolean (even after type juggling).
  • VALUE_IF_TRUE- value to be returned in case CONDITIONwas asserted to TRUE.
  • VALUE_IF_FALSE- value to be returned in case CONDITIONwas asserted to FALSE.
  • CONDITION- 可以是任何评估为布尔值的东西(即使在类型杂耍之后)。
  • VALUE_IF_TRUE-CONDITION被断言的情况下返回的值TRUE
  • VALUE_IF_FALSE-CONDITION被断言的情况下返回的值FALSE

回答by David Heffernan

That is the conditional operator. It is a ternary operator because it has three operands. It is often referred to as the ternary operator but that terminology is rather loose since any operator with three operands is a ternary operator. It just so happens that is is the only commonly used ternary operator.

那就是条件运算符。它是一个三元运算符,因为它具有三个操作数。它通常被称为三元运算符,但该术语相当松散,因为任何具有三个操作数的运算符都是三元运算符。碰巧它是唯一常用的三元运算符。

What does it mean? The expression

这是什么意思?表达方式

a?b:c

evaluates to bif aevaluates as true, otherwise the expression evaluates to c.

评估为bifa评估为真,否则表达式评估为c

回答by Robbie Ferrero

this is a ternary operator (the ?)

这是一个三元运算符(?)

Think of it like an IF statement.

把它想象成一个 IF 语句。

the statement before the '?' is the condition of your if statement. Immediately what follows before the ':' is what will execute/be-assigned if the statement is true. After the ':' is what will execute/be-assigned if the statement is false.

'?' 之前的语句 是 if 语句的条件。如果语句为真,紧跟在 ':' 之前的是将执行/分配的内容。如果语句为假,':' 之后将执行/分配。

Your code however will just alert 0 because you aren't assigning anything from your ternary operator.

但是,您的代码只会发出 0 警报,因为您没有从三元运算符中分配任何内容。

basically your code might as well say.
x = 0; alert(x); // this would alert 0

基本上你的代码还不如说。
x = 0; alert(x); // this would alert 0

you need to revise this to:
x = 0; var y = x > 0 ? 1 : -1; alert(y);

您需要将其修改为:
x = 0; var y = x > 0 ? 1 : -1; alert(y);

回答by CambridgeMike

It will be -1. This is known as the ternary operator.

它将是-1。这被称为三元运算符

Basically it expands to this (assuming you meant to put x=at the beginning of the second line).

基本上它扩展到这个(假设你打算放在x=第二行的开头)。

if(x>0){
  x = 1
} else {
  x = -1
}