JS--Logical AND (&&) 逻辑与
2018-06-24 00:24:59来源:未知 阅读 ()
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.
这都取决于左边的操作数。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:实现数组和对象的深浅拷贝
- js判断客户端是iOS还是Android等移动终端的方法 2020-02-25
- Javascript的&&和||的另类用法 2020-01-07
- 用js传递当前页面的url,丢失了&后面的参数 解决办 2019-08-14
- 解决 bash: vue command not found 2019-08-14
- 前端笔记之微信小程序(四)WebSocket&Socket.io&am 2019-08-14
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash