JS--Logical AND (&&) 逻辑与

Logical AND (&&)

The && operator can be understood at three different levels. At the simplest level, when used with boolean operands, && performs the Boolean AND operation on

&& 操作符可以从三个层面来理解,最简单的一个层面,&&操作符用在布尔操作数上,

the two values: it returns true if and only if both its first operand and its second operand are true. If one or both of these operands is false, it returns false.

当两个操作数都是ture的时候,才返回true。任何一个操作数是false,返回都是false。

&& is often used as a conjunction to join two relational expressions:

&& 操作符 经常被用在连接两个关系表达式上:

x == 0 && y == 0 // x,y 都是零才是返回 ture

Relational expressions always evaluate to true or false, so when used like this, the && operator itself returns true or false.

关系表达式结果要么是true,要么是false,所以,&&用在他们中间,或者返回ture,或者返回false。

 

Relational operators have higher precedence than && (and ||), so expressions like these can safely be written without parentheses.

关系运算符的优先级比逻辑运算符&& (或者||)的级别高,所以不用加上括号。

But && does not require that its operands be boolean values. Recall that all JavaScript values are either “truthy” or “falsy.” (The falsy values are false, null, undefined,

但是 && 运算符并不要求操作数是布尔值。事实上,JS多有值中,要么是 “真值”,要么是“假值”。(假值有:false,null,undefined,0,-0,Nan,and,“”

0, -0, NaN, and "". All other values, including all objects, are truthy.)

。剩下的值,包括对象都是真值的范围了。)

The second level at which && can be understood is as a Boolean AND operator for truthy and falsy values. If both operands are truthy, the operator returns a truthy

理解 &&操作符的第二个层面是: 操作数 或者返回值 不再是布尔值,而是真、假值。如果两个操作数都是“真值”,运算符就返回“真值”。

value. Otherwise, one or both operands must be falsy, and the operator returns a falsy value. In JavaScript, any expression or statement that expects a boolean value

否则,只要任何一个操作数是“假值”,就运算符就返回“假值”。在JS中,那些期待布尔值的表达式或者语句中,

will work with a truthy or falsy value, so the fact that  && does not always return true or false does not cause practical problems.

同样会对等的处理“真值”或者“假值”。所以,事实上  && 并不是经常返回布尔值 true或者false,并没什么实际问题。

Notice that the description above says that the operator returns “a truthy value” or “a falsy value,” but does not specify what that value is. 

值得注意的是,上面说 && 操作符返回的是 “真值”或者“假值”,但是并没有指出返回的具体值指的是什么样的值。

For that, we need to describe && at the third and final level. This operator starts by evaluating its first operand, the expression on its left. If the value on the left is

为此,我们需要讲解 &&  操作符的最后一个层面。操作符第一步是对他的第一个操作数进行求值,也就是表达式的左边的操作数。如果这个左值是个“假的”

falsy, the value of the entire expression must also be falsy, so && simply returns the value on the left and does not even evaluate the expression on the right.

那么整个表达式就是假的,所以,&&  就直接返回这个左值,不在计算表达式右边的值了。

On the other hand, if the value on the left is truthy, then the overall value of the expression depends on the value on the right-hand side.

另一方面,如果左值是“真的”,那么整个表达式的值就依赖于表达式右边的值了。

If the value on the right is truthy, then the overall value must be truthy, and if the value on the right is falsy, then the overall value must be falsy. So when the value on

如果右值是“真的”,那么整个表达式就是真的,如果右值是假的,那么整个表达式就是假的。所以,

the left is truthy, the && operator evaluates and returns the value on the right:

如果左边的值是真的,那么 && 运算符对右边操作数求值,并返回所求的值:
var o = { x : 1 };
var p = null;
o && o.x // => 返回是1: o 是真的, 所以对右边o.x求值,并返回 这个值
p && p.x // => 返回是null: p是假的, 所以直接返回,不用再对 p.x求值

It is important to understand that && may or may not evaluate its right-side operand. In the code above, the variable p is set to null, and the expression p.x would, if

明白 && 操作符 有可能不对右边的操作数求值很重要。在上面的代码中,如果变量p 是null,如果再对p.x进行求值,就会引起TypeError错误。

evaluated, cause a TypeError. But the code uses && in an idiomatic way so that p.x is evaluated only if p is truthy—not null or undefined.

在这样的惯用方式中,操作符&& 只有当左边操作数是真的,才会计算右边操作符。

The behavior of && is sometimes called “short circuiting,” and you may sometimes see code that purposely exploits this behavior to conditionally execute code.

所以  && 操作符 有时候被称作有 短路 的功能。你也会看到,有时候代码就是利用这个特性来有条件性的执行代码。

 

For example, the following two lines of JavaScript code have equivalent effects:

如下的例子,两行代码的效果是一样的:

if (a == b) stop(); // 调用 stop() ,只有在 a == b时发生
(a == b) && stop(); // 不一样的写法,同样的效果

In general, you must be careful whenever you write an expression with side effects (assignments, increments, decrements, or function invocations) on the right-hand

通常,必须小心所写的表达式对于 &&运算符右边的操作数是否有副作用,

side of  &&. Whether those side effects occur depends on the value of the left-hand side.

这都取决于左边的操作数。

posted @ 2017-09-30 10:59  流畅的心情  阅读(4894)  评论(0编辑  收藏  举报