Javascript javascript中对象值中的两条垂直线是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10463025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:25:38  来源:igfitidea点击:

What do two vertical lines in an object value mean in javascript?

javascriptoperators

提问by tim peterson

Possible Duplicate:
What does the || operator do?

可能的重复:
|| 是什么?运营商做什么?

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 oroperator and if so, how does it make sense that a variable can have two different values?

它是一个or运算符,如果是,一个变量可以有两个不同的值有什么意义?

回答by apsillers

This is an ORoperator. What you need to understand is:

这是一个OR运算符。你需要了解的是:

  • Non-boolean values are converted to a boolean when used in a logic operator. Values that convert to falseare called "falsy" and values that convert to trueare called "truthy". Falsy values include things like 0, undefined, null, and so on. See more at Truthy and Falsy: When All is Not Equal in JavaScript.

  • The ORoperator short-circuits: it keeps evaluating expressions until it finds on that is true, and then stops.

  • 在逻辑运算符中使用时,非布尔值将转换为布尔值。转换为的值false称为“falsy”,转换为的值true称为“truthy”。假值包括诸如0undefinednull等。在“ Truthy and Falsy: When All is Not Equal in JavaScript”中查看更多信息。

  • OR运营商短路:它使计算表达式,直到它发现是true,然后停止。

So, var time = $(el).data('start') || new Date();means "set timeto the startdata of the elelement, OR, if that's falsy, use the current time".

所以,var time = $(el).data('start') || new Date();意思是“设置time为元素的start数据el,或者,如果那是假的,使用当前时间”。

回答by David says reinstate Monica

It means 'or'. In this instance it assigns the value of $(el).data('start')to the variable timeor, if that doesn't exist or instead returns false, it assigns instead the value returned from new Date(). Or, as more clearly noted by Malovolio, in comments:

它的意思是“或”。在这种情况下,它将 的值分配给$(el).data('start')变量,time或者,如果变量不存在或返回false,则分配从 返回的值new Date()。或者,正如 Malovolio 在评论中更清楚地指出的那样:

...if $(el).data('start')is "falsy" (that is, undefined, null, 0, false, an empty string, or NaN), then new Date()is evaluated and assigned to time.

...如果$(el).data('start')是“假”(即未定义、空、0、假、空字符串或 NaN),则new Date()计算并分配给time

The important aspect of a logical operator:

逻辑运算符的重要方面:

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

逻辑运算符通常与布尔(逻辑)值一起使用;当它们是时,它们返回一个布尔值。然而,&& 和 || 运算符实际上返回指定操作数之一的值,因此如果这些运算符与非布尔值一起使用,它们可能返回非布尔值。

References:

参考:

回答by K2xL

exp1 || exp2 

evaluates exp1. If exp1 is true then exp2 is not evaluated (known as short circuit evaluation). If exp1 returns false then exp 2 is evaluated. If exp1 OR exp2 is true then (exp1||exp2) evaluates as true.

评估 exp1。如果 exp1 为真,则不评估 exp2(称为短路评估)。如果 exp1 返回 false,则计算 exp 2。如果 exp1 OR exp2 为真,则 (exp1||exp2) 评估为真。

But in Javascript, you can set values using the operator.

但是在 Javascript 中,您可以使用运算符设置值。

a = something

if (prop)

a = prop

can be rewritten as

可以改写为

a = prop || something

回答by yoah

The way the operator || is evaluated is that if the first part is true-ish, it returned it. If the first part is false-ish, it returns the second. The above expressions is therefore equivalent to:

运算符 || 的方式 评估的是,如果第一部分为真,则返回它。如果第一部分是假的,则返回第二部分。因此,上述表达式等效于:

if ($(el).data('start')) {
  time = $(el).data('start');
} else {
  time = new Date();
}

回答by Denis Ermolin

It means logical sum. var time = $(el).data('start') || new Date();if $(el).data('start')will have undefinedor falsevalue then timewill have value from new Datefunction.

这意味着logical sumvar time = $(el).data('start') || new Date();if $(el).data('start')will have undefinedor falsevalue then timewill have value from new Datefunction. if will have or value then will have value from function. if will have or value then will have value from function. if will have or value then will have value from function