(myVar && foo()) 在 JavaScript 中意味着什么?
(myVar && foo())
上面的代码是什么意思?它相当于什么?
我认为它在一条线上运行。
(myVar && foo())
What does the above code mean? What is it equivalent to?
I think it runs on a single line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果
myvar
为 false,则该表达式的计算结果为myvar
;如果myvar
为 true,则该表达式的计算结果为foo()
。以下片段几乎相同。The expression evaluates to
myvar
ifmyvar
is falsey, andfoo()
ifmyvar
is truthy. The following snippets are nearly identical.它是一个表达式,相当于“如果 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.
仅当 myVar 不是 falsey 时才会调用 foo() 函数:这意味着它不能为 false、""、0、null 或 undefined。 (还有其他吗?)
更典型的是看到这样的示例:
这被解释如下...
“如果'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:
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."
该表达式巧妙地利用了布尔表达式中的短路。仅当 myVar 计算结果为
true
时,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
.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)当&&评估时首先评估左侧,即
myVar
,如果这是 true,则仅评估右侧,即foo()
。这是因为在语句 A && 中B 如果 A 为假,则表达式 A&&B 将始终计算为假。上述语句通常按以下方式使用:
也可以写为:
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:
this can also be written as: