(myVar && foo()) 在 JavaScript 中意味着什么?

发布于 2024-08-17 08:09:31 字数 97 浏览 5 评论 0原文

(myVar && foo())

上面的代码是什么意思?它相当于什么?

我认为它在一条线上运行。

(myVar && foo())

What does the above code mean? What is it equivalent to?

I think it runs on a single line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

小傻瓜 2024-08-24 08:09:31

如果 myvar 为 false,则该表达式的计算结果为 myvar;如果 myvar 为 true,则该表达式的计算结果为 foo()。以下片段几乎相同。

var x = (myvar && foo());

if(myvar){ var x = foo(); } else { var x = myvar; }

The expression evaluates to myvar if myvar is falsey, and foo() if myvar is truthy. The following snippets are nearly identical.

var x = (myvar && foo());

if(myvar){ var x = foo(); } else { var x = myvar; }
撩发小公举 2024-08-24 08:09:31

它是一个表达式,相当于“如果 myVar 不为 false,则运行函数 foo()”。
如果这样使用:var x = (myVar && foo()),结果将是:

如果 myVar 不为 false,x 将被设置为 foo() 的输出。如果 myVar 为 false,则 x 将设置为 myVar 的值。

it is an expression that equates to "if myVar is not falsey, run the function foo()".
If it's used like this: var x = (myVar && foo()), the result will be:

if myVar is not falsey, x will be set to the output of foo(). if myVar is falsey, then x will be set to the value of myVar.

我偏爱纯白色 2024-08-24 08:09:31

仅当 myVar 不是 falsey 时才会调用 foo() 函数:这意味着它不能为 false、""、0、null 或 undefined。 (还有其他吗?)

更典型的是看到这样的示例:

window.console && console.log("some helpful debug info");

这被解释如下...

“如果'window'变量有一个名为'console'的成员...”

(在'console'前面加上前缀很重要'window'。因为,如果 console 未定义,您将得到一个 JavaScript 错误而不是 false。)

“...在 'console' 对象上调用 'log' 方法。”

The foo() function will only be called if myVar is not falsey: meaning it can't be false, "", 0, null, or undefined. (Any others?)

It's more typical to see an example like this:

window.console && console.log("some helpful debug info");

This is interpreted as follows...

"If the 'window' variable has a member called 'console'..."

(It's important to prefix 'console' with 'window.' because, if console is undefined, you'll get a JavaScript error instead of false.)

"... invoke the 'log' method on the 'console' object."

一页 2024-08-24 08:09:31

该表达式巧妙地利用了布尔表达式中的短路。仅当 myVar 计算结果为 true 时,foo() 才会执行。

if (myVar) {
    foo();
}

更清楚了。在某些情况下,此类代码可能是有意义的,但样式检查器(如果您对此类事情感兴趣)会对其产生不满。请参阅 http://javascript.crockford.com/code.html。避免依赖分号插入和表达式语句是一个很好的做法。

编辑:请注意 var x = a && b(); 在某些情况下可能不是一个坏主意。然而,(a && b()) 本身在很多方面都是错误的。 (分号插入、表达式语句,除了语义上的神秘之外)

The expression is making clever use of short circuiting in boolean expressions. foo() will only execute if myVar evaluates to true.

if (myVar) {
    foo();
}

is much clearer. There are probably cases where this sort of code makes sense, but style checkers (if you're interested in that sort of thing) will throw a fit over it. See http://javascript.crockford.com/code.html. It's a good practice to avoid relying on semicolon insertion and expression statements.

edit: Note that var x = a && b(); probably isn't such a bad idea in certain contexts. However, (a && b()) by itself is wrong in several ways. (semicolon insertion, expression statement, aside from being semantically cryptic)

不寐倦长更 2024-08-24 08:09:31

当&&评估时首先评估左侧,即 myVar,如果这是 true,则仅评估右侧,即 foo()。这是因为在语句 A && 中B 如果 A 为假,则表达式 A&&B 将始终计算为假。

上述语句通常按以下方式使用:

var x = myVar && foo();

也可以写为:

if myVar is truee x = foo(), else x = false

when && are evaluated the left hand is evaluated first i.e. myVar, if this is true then only the right hand side is evaluated i.e. foo(). This is because in a statement A && B if A is false then the expression A&&B will always evaluate to false.

The above statement is usually used in the following way:

var x = myVar && foo();

this can also be written as:

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