在javascript中,对象值中的两条垂直线是什么意思? [英] What do two vertical lines in an object value mean in javascript?

查看:76
本文介绍了在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:

  • 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 to true are 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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆