Possible Duplicate:
What does the || operator do?
也许有人可以提供更好的代码片段,但是 ||
能做什么呢?是什么意思?:
var time = $(el).data('start') || new Date();
是or
吗?运算符,如果是这样,一个变量可以有两个不同的值有何意义?
最佳答案
这是一个OR
运算符。您需要了解的是:
非 bool 值在逻辑运算符中使用时会转换为 bool 值。转换为
false
的值称为“falsy”,转换为true
的值称为“truthy”。虚假值包括0
、undefined
、null
等。查看更多Truthy and Falsy: When All is Not Equal in JavaScript .OR
运算符短路:它不断计算表达式,直到找到true
,然后停止。
所以,var time = $(el).data('start') || new Date();
表示“将 time
设置为 el
元素的 start
数据,或者,如果这是假的,请使用当前时间”。
关于javascript - 对象值中的两条垂直线在 JavaScript 中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10463025/