在javascript中,对象值中的两条垂直线是什么意思? [英] What do two vertical lines in an object value mean in javascript?
问题描述
可能重复:
什么是||运营商吗?
也许有人可以提供更好的代码片段,但是 ||
表示以下内容?:
Maybe somebody can provide a better code snippet, but what does ||
mean in the following?:
var time = $(el).data('start') || new Date();
是还是
运算符,如果是,变量有两个不同的值有什么意义呢?
Is it an or
operator and if so, how does it make sense that a variable can have two different values?
推荐答案
这是一个或者
运营商。您需要了解的是:
This is an OR
operator. What you need to understand is:
-
在逻辑运算符中使用时,非布尔值将转换为布尔值。转换为
false
的值称为falsy,转换为true
的值称为truthy。 Falsy值包括0
,undefined
,null
,等等。有关详细信息,请参阅 Truthy and Falsy:JavaScript中的所有内容均不相同。
Non-boolean values are converted to a boolean when used in a logic operator. Values that convert to
false
are called "falsy" and values that convert totrue
are called "truthy". Falsy values include things like0
,undefined
,null
, and so on. See more at Truthy and Falsy: When All is Not Equal in JavaScript.
OR
运算符短路:它一直在评估表达式,直到找到 true
,然后停止。
The OR
operator short-circuits: it keeps evaluating expressions until it finds on that is true
, and then stops.
所以, var time = $(el).data('start')|| new Date();
表示设置时间
到 start
<$的数据c $ c> el 元素,或者,如果这是假的,请使用当前时间。
So, var time = $(el).data('start') || new Date();
means "set time
to the start
data of the el
element, OR, if that's falsy, use the current time".
这篇关于在javascript中,对象值中的两条垂直线是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!