javascript 简写 if 语句,没有 else 部分 [英] javascript shorthand if statement, without the else portion
本文介绍了javascript 简写 if 语句,没有 else 部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我使用了一个速记 javascript if
/else
语句(我在某处读到它们被称为三元语句?)
So I'm using a shorthand javascript if
/else
statement (I read somewhere they're called Ternary statements?)
this.dragHandle.hasClass('handle-low') ?方向 = "左" : 方向 = "右"
这很好用,但是如果以后我只想使用速记 if
而没有 else
部分怎么办.喜欢:
This works great, but what if later I want to use just a shorthand if
, without the else
portion. Like:
direction == "right" ?slideOffset += $(".range-slide").width()
这可能吗?
推荐答案
您可以使用 &&
运算符 - 只有在第一个为真时才执行第二个操作数表达式
you can use &&
operator - second operand expression is executed only if first is true
direction == "right" && slideOffset += $(".range-slide").width()
在我看来 if(conditon) 表达式
比 condition && 更具可读性表达式
这篇关于javascript 简写 if 语句,没有 else 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文