[英]Javascript returns string for OR(||) operation
I am unable to understand this. 我不明白这一点。
Following is expression uses OR operator 以下是表达式使用OR运算符
var subCond1 = adj.getData('relationEnabled') == 'true' || adj.getData('unhideIfHidden') || adj.getData('hlFixed') == 'true';
I am expecting that since it is OR operation, it should return boolean true/false, but instead I get string 'false' as a result. 我期望由于它是“或”运算,因此应该返回布尔值true / false,但是结果是字符串'false'。
Can anyone explain this please? 谁能解释一下?
Yup, that's just one of the features of ||
是的,这只是
||
的功能之一 in JavaScript, and it's deliberate. 在JavaScript中,这是故意的。 It doesn't return a boolean (necessarily), it works like this: It evaluates the left-hand operand and if that operand is truthy , it returns it;
它不返回布尔值(必需),它的工作方式如下:计算左操作数,如果该操作数为true ,则返回它; otherwise, it evalutes and returns the right-hand operand.
否则,它评估并返回右侧的操作数。
So what's "truthy"? 那么什么是“真实的”? Anything that isn't "falsy".
任何不是“虚假”的东西。 :-) The falsy values are
0
, ""
, null
, undefined
, NaN
, and of course, false
. :-)伪造的值为
0
, ""
, null
, undefined
, NaN
,当然还为false
。 Anything else is truthy. 其他任何事情都是真实的。
If you need a boolean, just !!
如果您需要布尔值,那就
!!
the result: 结果:
var subCond1 = !!(adj.getData('relationEnabled') == 'true' || adj.getData('unhideIfHidden') || adj.getData('hlFixed') == 'true');
...but you frequently don't need to bother. ...但是您经常不需要打扰。
This behavior of ||
||
这种行为 is really useful, particularly (I find) when dealing with object references that may be null
, when you want to provide a default: 确实很有用,特别是(我发现)在处理可能为
null
对象引用时,当您想提供默认值时:
var obj = thisMayBeNull || {};
Now, obj
will be thisMayBeNull
if it's truthy (non- null
object references are truthy), or {}
if thisMayBeNull
is falsy. 现在,
obj
将thisMayBeNull
,如果它的truthy(非null
对象引用truthy),或{}
如果thisMayBeNull
是falsy。
More in this article on my blog: JavaScript's Curiously-Powerful OR Operator ( ||
) 在我的博客上,本文的更多内容: JavaScript的强大OR运算符(
||
)
Just to round things out: The &&
operator has a similar behavior: It evaluates the left-hand operand and, if it's falsy , returns it; 只是为了解决问题:
&&
运算符具有类似的行为:它计算左侧的操作数,如果它是伪造的 ,则将其返回; otherwise it evaluates and returns the right-hand operator. 否则,它求值并返回右手运算符。 This is useful if you want an object property from a variable which may be
null
: 如果要从可能为
null
的变量获取对象属性,这将非常有用:
var value = obj && obj.property;
value
will be the value of obj
if obj
is falsy (for instance, null
), or the value of obj.property
if obj
is truthy. value
将是的值obj
如果obj
是falsy(例如, null
),或者值obj.property
如果obj
是truthy。
Javascript returns the first operand that has a truthy value, whatever that truthy value is or the value of the last operand if none before are truthy. Javascript返回具有真实值的第一个操作数,无论该真实值是多少,或者如果之前没有一个是真实的,则返回最后一个操作数的值。 That is a designed feature of Javascript (yes it is different than other languages).
那是Javascript的设计功能(是的,它与其他语言不同)。
You can turn that into a boolean by comparing to see if it is == true
if you want or it is sometimes done with !!
您可以通过比较看看是否为
== true
来将其转换为布尔值,或者有时用!!
完成!!
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.