什么是||操作员呢? [英] What does the || operator do?
问题描述
Attacklab.wmd_env.buttons=Attacklab.wmd_env.buttons||_4;
什么是||在这种情况下做什么?
将_4添加到Attacklab.wmd_env.buttons数组?
what does the || do in this case?
Adds _4 to the array which is Attacklab.wmd_env.buttons?
推荐答案
JavaScript中的 ||
运算符返回左侧的值,如果该值未计算为 false
,否则返回右边的值。
The ||
operator in JavaScript returns the value on the left if that value does not evaluate to false
, otherwise it returns the value on the right.
来自 Mozilla的核心JavaScript 1.5参考:
expr1 || expr2
如果可以转换为true,则返回expr1
;否则,返回expr2
。因此,当与布尔值一起使用时,如果任一操作数为真,则||
返回true;如果两者都为假,则返回false。
expr1 || expr2
Returnsexpr1
if it can be converted to true; otherwise, returnsexpr2
. Thus, when used with Boolean values,||
returns true if either operand is true; if both are false, returns false.
因此,在这种情况下,如果 Attacklab.wmd_env.buttons
没有值,它将值设置为 _4
。
So, in this case, if Attacklab.wmd_env.buttons
doesn't have a value, it sets the value to _4
.
这篇关于什么是||操作员呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!