[英]Why | and || are similar to a semicolon in Javascript?
I was testing some stuff on Console on Chrome and then I ran that piece of code:我在 Chrome 上的控制台上测试了一些东西,然后我运行了这段代码:
alert() | window.confirm(); alert() || window.confirm();
My problem was to run both alert and confirm methods in a single line without using semicolon.我的问题是在一行中同时运行 alert 和 confirm 方法而不使用分号。 It turns out that both |
事实证明,两者 | and ||
和 || worked and I can't imagine why it worked, firstly because ||
有效,我无法想象它为什么有效,首先是因为|| means an OR operator which should run one of them not both and secondly |
表示一个 OR 运算符,它应该运行其中一个,而不是两者,其次| I don't know what it is.
我不知道它是什么。 Can someone explain what happened and what else should work instead of ;
有人可以解释发生了什么,还有什么应该起作用而不是; ?
?
alert()
returns undefined
, which is false-y. alert()
返回undefined
,这是 false-y。 Therefore, window.confirm()
will still run, for your example of ||
因此,对于您的
||
示例, window.confirm()
仍将运行. .
As for a single pipe character |
至于单管字符
|
, that's a bitwise-OR, which you can read about here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Bitwise ,这是一个按位或,你可以在这里阅读: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Bitwise
A semicolon indicates the end of a statement.分号表示语句的结束。
If you aren't already aware, an expression is something which evaluates to a value.如果您还不知道,表达式就是计算结果为值的东西。 For example,
5
, 'foobar'
, and myFn()
are all expressions, because they evaluate to values.例如,
5
、 'foobar'
和myFn()
都是表达式,因为它们的计算结果为值。
Statements can be composed of multiple expressions.语句可以由多个表达式组成。 For example,
const result = fn('foo')
passes the 'foo'
expression into the function call, and the function call returns a value which is assigned to result
.例如,
const result = fn('foo')
将'foo'
表达式传递给函数调用,函数调用返回一个分配给result
的值。
In your code, both lines are composed of two expressions, but each line happens to be a single statement.在您的代码中,两行都由两个表达式组成,但每一行恰好是一个语句。 With this line:
有了这条线:
alert() || window.confirm()
will first evaluate alert
.将首先评估
alert
。 Since alert
returns undefined
, the ||
由于
alert
返回undefined
, ||
operator then evaluates the expression on the right, which is window.confirm()
.运算符然后计算右侧的表达式,即
window.confirm()
。
You can put multiple expressions together on the same by using operators like |
您可以使用像
|
这样的运算符将多个表达式放在一起。 , ||
,
||
, or =
. , 或
=
。 You can also evaluate multiple expressions by putting each as a separate statement, like您还可以通过将每个表达式作为单独的语句来评估多个表达式,例如
alert();
window.confirm();
Both will result in an alert box and a confirm dialog coming up.两者都会导致出现一个警告框和一个确认对话框。
The ||
||
is an operator, like +
or /
;是一个运算符,如
+
或/
; it calculates something.它计算一些东西。 In the case of
||
在
||
的情况下, it calculates whether one OR the other value is true. ,它计算一个或另一个值是否为真。
Normally, you'd use that in something like an if
statement: if (i===0 || j===0) {...}
but it's not restricted to that;通常,您会在类似
if
语句的情况下使用它: if (i===0 || j===0) {...}
但不限于此; for instance, you could put the result in a variable, then use it in an if
statement later: have_zero = i===0 || j===0; ...; if (have_zero) {...}
例如,您可以将结果放在一个变量中,然后稍后在
if
语句中使用它: have_zero = i===0 || j===0; ...; if (have_zero) {...}
have_zero = i===0 || j===0; ...; if (have_zero) {...}
The ||
||
(and &&
) operators do have one special thing: if the left side determines the answer, they don't bother calculating the right side (called "short-circuit evaluation"). (and
&&
) 运算符确实有一件特别的事情:如果左侧确定了答案,则它们不会费心计算右侧(称为“短路评估”)。
Here, you're calculating alert() || window.confirm()
在这里,您正在计算
alert() || window.confirm()
alert() || window.confirm()
, so it calls alert()
; alert() || window.confirm()
,所以它调用alert()
; as others have noted, this returns undefined
which doesn't determine the answer to the ||
正如其他人所指出的,这将返回
undefined
这并不能确定||
的答案, so Javascript then calls window.confirm()
. ,所以 Javascript 然后调用
window.confirm()
。 The answer is then thrown away, because you're not putting it in a variable or otherwise using it, but that's OK - you wanted to call the methods, you weren't interested in the answer.然后答案被丢弃,因为您没有将它放在变量中或以其他方式使用它,但这没关系 - 您想调用方法,您对答案不感兴趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.