What does this javascript code do? [duplicate]

↘锁芯ラ 提交于 2019-12-25 00:38:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!