设为首页 加入收藏

TOP

JS--Logical AND (&&) 逻辑与(一)
2017-10-10 12:32:56 】 浏览:9290
Tags: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 eva luate 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 java script 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 java script, 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 eva luating 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 eva luate 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 &&a

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇实现数组和对象的深浅拷贝 下一篇全面实用的Tab切换

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目