What does this javascript code do? [duplicate]
y = x?0:0x80
开发者_如何学CFrom googling the colon seems to be a ternary operator.
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;
}
It is a ternary operator. It assigns 0 to y if x is true, otherwise it assigns 0x80.
Yes, it's a ternary operation, assigning y
the value of 0
if x
is true
or 0x80
otherwise.
It translates to:
if(x) then
y=0
else
y=0x80
But is much shorter.
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
.
精彩评论