问题
y = x?0:0x80
From googling the colon seems to be a ternary operator.
回答1:
That's right. (The correct name is the conditional operator. It is a ternary operator, in that it takes three operands, but it's commonly misnamed the ternary operator because it's the only JavaScript operator that does so.)
The code is roughly equivalent to this:
var y;
if (x) {
y = 0;
}
else {
y = 0x80;
}
回答2:
It is a ternary operator. It assigns 0 to y if x is true, otherwise it assigns 0x80.
回答3:
Yes, it's a ternary operation, assigning y
the value of 0
if x
is true
or 0x80
otherwise.
回答4:
It translates to:
if(x) then
y=0
else
y=0x80
But is much shorter.
回答5:
Yes to all of the above, but it is also checking for the existence of x. If x doesn't exist or is null
, y = 0x80
.
来源:https://stackoverflow.com/questions/6813840/what-does-this-javascript-code-do