假的? && 是如何实现的?运算符导致 0 返回 0 而不是 NaN?
我读到有时 &&运算符用于“短路”JavaScript,使其相信返回值 0 是 0 而不是 NaN,因为 0 在 JavaScript 中是一个假数。我一直在环顾四周,想弄清楚这一切意味着什么。有人可以向外行解释一下吗?
例如:
function sign(number) {
return number && number / Math.abs(number); }
如果 number
为 0,则返回 0。
I read that sometimes the && operator is used to "short circuit" JavaScript into believing that a return value of 0 is 0 and not NaN because 0 is a falsy number in JavaScript. I've been looking around to figure out what all this means. Can someone explain it to a layman?
For example:
function sign(number) {
return number && number / Math.abs(number); }
Will return 0 if number
is 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这听起来像是我之前看到的答案,但现在找不到(链接会有所帮助),但你在这里得到了错误的答案。
给定这样的情况:
在第二行, foo 将被评估。它是
0
,因此是一个假值。&&
(foo
) 的左侧将被返回并分配给bar
。现在,如果我们有
foo = 1
:同样,
foo
将被评估。它是一个真值,因此&&
的右侧将被计算并返回。2 / foo
是2
,因此2
被分配给bar
。“短路”只是意味着一旦
&&
的一部分失败,它就会返回失败的部分,而不评估右侧的任何内容。This sounds like an answer that I saw earlier, but cannot find now (a link would be helpful), but you've got the wrong end of the stick here.
Given a situation like:
On line two, foo will be evaluated. It is
0
and therefore a false value. The left hand side of&&
(foo
) will be returned and assigned tobar
.Now if we have
foo = 1
:Again,
foo
will be evaluated. It is a true value, so the right hand side of&&
will be evaluated and returned.2 / foo
is2
so2
is assigned tobar
."Short circuit" just means that as soon as part of
&&
fails then it returns the part the failed without evaluating anything to the right.在 JavaScript 中,布尔运算符
&&
和||
不一定返回布尔值。相反,他们会审视自己论点的“真实性”,并可能相应地短路。有些值如0
、空字符串""
和null
是“falsy”。短路只是意味着跳过表达式右侧的求值,因为左侧足以提供答案。
例如:像
var result = 100 / number;
这样的表达式,当number = 0
时,将为您提供NaN
,但是:将为您提供
0
而不是NaN
,因为0
是假的。在布尔上下文中false && Anything
为false
,因此评估右侧是没有意义的。类似地:如果字符串
msg
不为空(true),则会给您msg
,因为true ||任何内容
都是true
。如果msg
为空,则会显示“No messagerather”
。In JavaScript, the boolean operators
&&
and||
don't necessarily return a boolean. Instead, they look at the "truthiness" of their arguments and might short circuit accordingly. Some values like0
, the empty string""
andnull
are "falsy".Short circuiting just means skip the evaluation of the right hand side of an expression because the left hand side is enough to provide the answer.
For example: an expression like
var result = 100 / number;
will give youNaN
whennumber = 0
, but:Will give you
0
instead of aNaN
since0
is falsy. In a boolean contextfalse && anything
isfalse
, so there's no point in evaluating the right hand side. Similarly:Will give you
msg
if the stringmsg
is not empty (truthy) sincetrue || anything
istrue
. Ifmsg
is empty, it gives you"No message instead"
.我想我明白你的意思,但事实并非如此。
现在,当 func_a 返回 false 时,func_b 确实不会被调用。然而,当它返回 NaN(就像 0 一样,等于 false)时,它仍然返回 false!
I think I understand what you mean, however, it's not.
Now, when func_a returns false, func_b indeed doesn't get called. However, when it returns NaN (which is, just like 0, equal to false) it still returns false!