简体   繁体   English

&&在JavaScript表达式中返回什么

[英]what does && return in javascript expression

I was reading the javascipt code in some application and code was this 我在某些应用程序中正在阅读javascipt代码,代码是这样的

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

Now i am confused what it will return. 现在我很困惑它将返回什么。

IT looks to me like 在我看来

return a&&b

won't it return true or false rather than b 它不会返回true还是false而不是b

Logical AND ( && ): 逻辑AND( && ):
expr1 && expr2 Returns expr1 if it can be converted to false; expr1 && expr2如果可以将其转换为false,则返回expr1否则返回false。 otherwise, returns expr2 . 否则,返回expr2 Thus, when used with Boolean values, && returns true if both operands are true ; 因此,利用布尔值使用时, &&返回true ,如果两个操作数都是true ; otherwise, returns false . 否则,返回false

Source 资源

So, basically: 因此,基本上:
If the first parameter is falsy , it returns that parameter. 如果第一个参数为falsy ,则返回该参数。 Else, it literally returns the second parameter. 否则,它从字面上返回第二个参数。

In your case, this means that, if this.grid exists, it returns this.grid.getStore().sum('fees'); 在您的情况下,这意味着,如果存在this.grid ,则返回this.grid.getStore().sum('fees');

You misunderstand what && does. 您误解了&&功能。 Let a and b be "entities". ab为“实体”。 Then a && b does: 然后a && b执行:

  1. evaluate a 评估a
  2. if a is falsy return a 如果a虚假,则返回a
  3. if a is truthy evaluate b 如果a为真,则评估b
  4. return b 返回b

Example: 例:

var f = function() {
    console.log("test");
    return 'foo';
}

> 0 && f()
0
> 1 && f()
test
"foo"

Note that in first case we didn't get console.log because f() was not evaluated because 0 is falsy. 请注意,在第一种情况下,我们没有得到console.log因为没有评估f() ,因为0是虚假的。 This property is important and actually 此属性很重要,实际上

a && b != b && a

even though mathematically it should be the same (but it is not due to side-effects of evaluation). 即使从数学上讲应该是相同的(但这不是由于评估的副作用)。

Falsy values include: 0 , false , "" (empty string), null , undefined , NaN (not a number type). false值包括: 0false"" (空字符串), nullundefinedNaN (不是数字类型)。 I don't think there are any other possible values (someone correct me if I'm wrong). 我认为没有其他可能的值(如果我错了,请纠正我)。 Every other object is truthy. 其他所有对象都是真实的。

So in your case the code can be rewritten as: 因此,在您的情况下,代码可以重写为:

if (this.grid) {
    return this.grid.getStore().sum('fees');
} else {
    return this.grid;
}

This is done to protect against calling a method on undefined property, witch would cause an error. 这样做是为了防止在未定义的属性上调用方法,否则会导致错误。 So if this.grid is undefined , then undefined is returned. 因此,如果this.gridundefined ,则返回undefined

In expressions if a && b when a equals to false (or in javascript it can be an expression like in Cerburs answer), then a is returned. 在表达式中,如果a && ba等于false (或在javascript它可像在Cerburs答案的表达式),则a被返回。

Similarly with || ||类似 operator, the first from the left that equals to true (in javascript not 0, not undefined, not null, not NaN, and not false of course) is returned. 运算符,则返回第一个等于true (在javascript中不是0,不是undefined,不是null,不是NaN,当然也不是false)。

Ok, let's assume that this.grid.getStore().sum('fees') returns something, let's say "okay!". 好的,我们假设this.grid.getStore().sum('fees')返回了一些东西,比如说“好的!”。

now the return statement in your code is a convoluted way of saying : 现在,代码中的return语句是一种复杂的说法:

if(this.grid)//this.grid is defined and doesn't evaluate as 'false'
    return this.grid.getStore().sum('fees');
else
    return this.grid;

if this hasn't got a grid, we return undefined, else we call gridStore... and return its own return. 如果没有网格,则返回undefined,否则调用gridStore...并返回其自身的返回值。 It is a common way of avoiding "undefined has no method 'gridStore'" 这是避免“未定义的没有方法'gridStore'”的常用方法

the VERY important part is that, in a && f() , f() will NOT be called if 'a' evaluates to false. 非常重要的一点是,在a && f() ,如果'a'的计算结果为false,则不会调用f()。 there are many things that evaluate to false, such as any undefined variable, null , empty strings... (note that strings that contain falsy things like "false" or "0000" are actually truthy). 有很多评估结果为false的东西,例如任何未定义的变量, null ,空字符串...(请注意,包含虚假的东西(例如"false""0000" )的字符串实际上是真实的)。 or even unreadable babble like function(){return null;}(); 甚至是像function(){return null;}();类的无法理解的ba语 may evaluate as false. 可能评估为错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM