此Javascript行中有两个管道符号(OR) [英] Two pipe symbols (OR) in this Javascript line

查看:104
本文介绍了此Javascript行中有两个管道符号(OR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

什么是“ options = options || {}”的在Javascript中意味着什么?

我在JS中看到了这一点:

I have seen this in JS:

item = item || {};

我猜它是三元运算符的一些变体,但实际上是做什么的?

I'm guessing it's some variation of a ternary operator but what does is actually do?

推荐答案

(expr1 || expr2)




如果可以转换为真,则返回expr1;否则,返回
expr2。

"Returns expr1 if it can be converted to true; otherwise, returns expr2."

source

所以当 expr1 是(或评估为)其中一个 0时,,false,null,undefined,NaN ,然后返回 expr2 ,否则 expr1 被退回

So when expr1 is (or evaluates to) one of these 0,"",false,null,undefined,NaN, then expr2 is returned, otherwise expr1 is returned

这篇关于此Javascript行中有两个管道符号(OR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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