gpt4 book ai didi

javascript - && 在 javascript 表达式中返回什么

转载 作者:行者123 更新时间:2023-11-30 10:14:54 25 4
gpt4 key购买 nike

我正在阅读某些应用程序中的 javascipt 代码,代码是这样的

getTotalFees:function(){
return this.grid
&&this.grid.getStore().sum('fees');
}

现在我很困惑它会返回什么。

它看起来像

返回a&&b

它会不会返回 true 或 false 而不是 b

最佳答案

Logical AND (&&):
expr1 && expr2 Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

Source

所以,基本上:
如果第一个参数是 falsy,则返回该参数。否则,它会直接返回第二个参数。

在你的例子中,这意味着,如果 this.grid 存在,它返回 this.grid.getStore().sum('fees');

关于javascript - && 在 javascript 表达式中返回什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24403011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com