[英]Javascript var === 2 && console.log('true'). How does this work?
var test = 2;
test === 2 && console.log('true');
This confuses me. 这使我感到困惑。 I hadn't seen this before until the other day. 直到第二天我才看到这个。 The output of this will be true
in the console. 此输出在控制台中为true
。
So how does this magic work? 那么,这种魔术如何起作用?
This (mis)uses the boolean handling of JavaScript; 这(错误)使用了JavaScript的布尔值处理; because of the lazy evaluation of expressions, console.log('true')
is only run, when someVar === 2
was previously evaluated as true
. 由于表达式的延迟计算,仅当someVar === 2
先前被评估为true
时,才会运行console.log('true')
。
This behaviour is called Short-circuit evaluation . 这种行为称为短路评估 。
By the way, this type of coding has legit use cases. 顺便说一下,这种类型的编码具有合法的用例。 My favourite is the use of configuration objects : 我最喜欢的是使用配置对象 :
function (options) {
var options = options || {};
}
This way, the options object exists even if it was not given in as a parameter. 这样,即使没有将其作为参数提供,options对象也存在。
Actually, this code isn't valid: 实际上,此代码无效:
var === 2 && console.log('true')
VM132:2 Uncaught SyntaxError: Unexpected token ===
You can try it yourself in the browser (ie in Chrome look for Developer Tools). 您可以在浏览器中自行尝试(例如,在Chrome浏览器中查找开发人员工具)。
See http://jsfiddle.net/jcdxnte0/ 参见http://jsfiddle.net/jcdxnte0/
EDIT: 编辑:
The syntax x && y
means that both x
and y
need to be true. 语法x && y
表示x
和y
必须为真。 This means, that if x
is true, y
will get evaluated, and if x
is false, y
will not get evaluated (because x
was false, so both will never be true so why bother evaluating y
? - evaluation goes from left to right). 这意味着,如果x
为true,则y
将被求值,如果x
为false,则y
将不被求值(因为x
为false,所以两个都永远都不为真,所以为什么要费心计算y
?-评估从左到右进行)。 In your case, x
is test === 2
, and y
is console.log(...)
. 在您的情况下, x
是test === 2
,而y
是console.log(...)
。
it can be read as 它可以读为
if( test === 2 ){ console.log('true'); }
It works because (test === 2)
is an expression which resolves to a value . 之所以起作用,是因为(test === 2)
是一个可解析为value的表达式 。
It'll resolve to either true or false. 它将解析为true或false。 JavaScript will then execute code after &&
only if the preceding expression returned true. 然后,仅当前面的表达式返回true时,JavaScript才会在&&
之后执行代码。 (so false && console.log()
won't log anything). (因此false && console.log()
将不会记录任何内容)。
Another fun way to abuse it and confuse other developers is the fact that assignment also returns a value, for example 滥用它并使其他开发人员感到困惑的另一种有趣方式是,分配也返回一个值,例如
var x = 0, y = 5;
(x = y) && console.log( 'x is now 5' );
in this case (x = y)
not only assigns the value of y
to x
, but also returns 5
which is truthy, so the console.log
gets evaluated. 在这种情况下(x = y)
不仅将y
的值分配给x
,而且还返回5
,这是正确的,因此console.log
得到评估。 don't use this, please . 请不要使用这个 。
JavaScript evaluates the left part and, if it's true, executes the right part. JavaScript评估左边的部分,如果是,则执行右边的部分。 You can do the opposite with the ||
您可以使用||
进行相反的操作 operator. 操作员。
It's also worth noting that the right part should be wrapped in parenthesis if it contains an assignment: 还值得注意的是,如果正确的部分包含赋值,则应将其包裹在括号中:
var foo;
true && (foo = "bar");
While it's perfectly valid, I'd recommend not using this syntax. 虽然完全有效,但我建议不要使用此语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.