Javascript 有两个变量的JS决策是否可以以任何优雅的方式减少,而不是使用IF语句?
我是一名JS新手,虽然下面的代码可以很好地处理2个变量和IF语句,但我想知道,没有IF语句,有没有什么优雅的方法可以做到这一点 工作代码如下:Javascript 有两个变量的JS决策是否可以以任何优雅的方式减少,而不是使用IF语句?,javascript,if-statement,Javascript,If Statement,我是一名JS新手,虽然下面的代码可以很好地处理2个变量和IF语句,但我想知道,没有IF语句,有没有什么优雅的方法可以做到这一点 工作代码如下: let animation; if(!detectScroll && !detectHover){ animation = loadAnimation(reverseAnimationData) } else if(detectScroll && !detectHover){
let animation;
if(!detectScroll && !detectHover){
animation = loadAnimation(reverseAnimationData)
}
else if(detectScroll && !detectHover){
animation = loadAnimation(cyanAnimationData)
}
else if(!detectScroll && detectHover){
animation = loadAnimation(cyanAnimationData)
}
else {
animation = loadAnimation(cyanAnimationData)
}
我想知道,在没有IF语句的情况下,我是否可以用任何其他方法将代码缩减为更少的代码行。您可以试试
let animation = loadAnimation(!detectScroll && !detectHover ? reverseAnimationData : cyanAnimationData )
它适用于此场景,但如何处理更通用的场景?