假的? && 是如何实现的?运算符导致 0 返回 0 而不是 NaN?

发布于 2024-12-07 12:40:40 字数 291 浏览 0 评论 0原文

我读到有时 &&运算符用于“短路”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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔嚣张 2024-12-14 12:40:40

这听起来像是我之前看到的答案,但现在找不到(链接会有所帮助),但你在这里得到了错误的答案。

给定这样的情况:

foo = 0 ;
bar = foo && 2 / foo;

在第二行, foo 将被评估。它是 0,因此是一个假值。 && (foo) 的左侧将被返回并分配给 bar

现在,如果我们有 foo = 1

foo = 1 ;
bar = foo && 2 / foo;

同样,foo 将被评估。它是一个真值,因此 && 的右侧将被计算并返回。 2 / foo2,因此 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:

foo = 0 ;
bar = foo && 2 / foo;

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 to bar.

Now if we have foo = 1:

foo = 1 ;
bar = foo && 2 / foo;

Again, foo will be evaluated. It is a true value, so the right hand side of && will be evaluated and returned. 2 / foo is 2 so 2 is assigned to bar.

"Short circuit" just means that as soon as part of && fails then it returns the part the failed without evaluating anything to the right.

魔法少女 2024-12-14 12:40:40

在 JavaScript 中,布尔运算符 &&|| 不一定返回布尔值。相反,他们会审视自己论点的“真实性”,并可能相应地短路。有些值如 0、空字符串 ""null 是“falsy”。

短路只是意味着跳过表达式右侧的求值,因为左侧足以提供答案。

例如:像 var result = 100 / number; 这样的表达式,当 number = 0 时,将为您提供 NaN,但是:

var result = number && 100 / number;

将为您提供 0 而不是 NaN,因为 0 是假的。在布尔上下文中 false && Anythingfalse,因此评估右侧是没有意义的。类似地:

// supposed msg is a potentially empty string
var message = msg || "No message";

如果字符串 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 like 0, the empty string "" and null 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 you NaN when number = 0, but:

var result = number && 100 / number;

Will give you 0 instead of a NaN since 0 is falsy. In a boolean context false && anything is false, so there's no point in evaluating the right hand side. Similarly:

// supposed msg is a potentially empty string
var message = msg || "No message";

Will give you msg if the string msg is not empty (truthy) since true || anything is true. If msg is empty, it gives you "No message instead".

鱼忆七猫命九 2024-12-14 12:40:40

我想我明白你的意思,但事实并非如此。

var some_bool = (func_a() && func_b());

现在,当 func_a 返回 false 时,func_b 确实不会被调用。然而,当它返回 NaN(就像 0 一样,等于 false)时,它仍然返回 false!

I think I understand what you mean, however, it's not.

var some_bool = (func_a() && func_b());

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文