什么和&&在javascript表达式中返回 [英] what does && return in javascript expression
问题描述
我正在阅读某些应用程序中的javascipt代码,代码是这个
I was reading the javascipt code in some application and code was this
getTotalFees:function(){
return this.grid
&&this.grid.getStore().sum('fees');
}
现在我很困惑它将返回什么。
Now i am confused what it will return.
IT对我来说就像
返回&& b
不会返回true或false而不是b
won't it return true or false rather than b
推荐答案
逻辑AND(
&&
):
expr1&& expr2
如果可以转换为false,则返回expr1
;否则,返回expr2
。因此,当与布尔值一起使用时,如果两个操作数都是,
;否则,返回&&
将返回true
真false
。
Logical AND (
&&
):
expr1 && expr2
Returnsexpr1
if it can be converted to false; otherwise, returnsexpr2
. Thus, when used with Boolean values,&&
returnstrue
if both operands aretrue
; otherwise, returnsfalse
.
所以,基本上:
如果第一个参数是 falsy
,它会返回该参数。否则,它确实返回第二个参数。
So, basically:
If the first parameter is falsy
, it returns that parameter. Else, it literally returns the second parameter.
在您的情况下,这意味着,如果 this.grid
存在,它返回 this.grid.getStore()。sum('fees');
In your case, this means that, if this.grid
exists, it returns this.grid.getStore().sum('fees');
这篇关于什么和&&在javascript表达式中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!