解释javascript代码 [英] explain javascript code

查看:65
本文介绍了解释javascript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以解释一下这段代码



can you please explain this code

var demo = (y1 & 1) ? x1 + 0.5 : x1;





或将其转换为python因为我知道python



谢谢



or convert it to python because i know python

thank you

推荐答案

为什么不阅读一些文档并尝试理解它?

条件(三元)运算符(?:) [ ^ ]

JavaScript运算符 [ ^ ]
why not read some documentation and try to understand it?
Conditional (Ternary) Operator (?: ) [^]
JavaScript Operators[^]


解决方案2不太正确。



更正后的伪代码为:

Solution 2 is not quite correct.

The corrected pseudo-code is:
if (first bit (#0) in y1 is set)
{ // if this bit is set:
    demo = x1 + 0.5;
} 
else 
{ // if this bit is clear:
    demo = x1;
}



位检查由按位AND运算符'&'完成:测试编号 y1 与安装了单个位的位掩码(1,2,4,8等)进行比较,结果为0或者不是。

现在,诀窍是:如果操作数 if 是一个数字,如果数字为0,则表现为False,在所有其他情况下为True;这是一个肮脏的C式约定。



请参阅,例如, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators [ ^ ]。



-SA


The bit check is done by the bitwise AND operator '&': test number y1 is compared with a bit mask with a single bit installed (1, 2, 4, 8, etc.) and the result is either 0 or not.
Now, the trick is: if the operand of "if" is a number, it behaves as False if the number is 0, True in all other cases; this is a dirty C-like convention.

Please see, for example, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators[^].

—SA


我不确定这是否真的与Python或JavaScript相关。当你宣布你的Python(或任何其他编程语言)时,它应该是可以理解的。



无论如何,这里是伪代码:



I am not sure if this is really Python or JavaScript related. And as you proclaimed your Python (or any other programming language), it should have been understandable.

Anyway, here is the pseudo-code:

if (y1 & 1) is true
{
	demo = x1 + 0.5;
}
else
{
	demo = x1;
}


这篇关于解释javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆