JavaScript if语句的最简单方式

时间:2012-07-30 22:23:27

标签: javascript if-statement

好的,我知道这是一团糟。这就是我在这里发布的原因。我希望你能理解我想要创造的逻辑。是否有更好的方法来实现这一目标?

else if (((!(lc(this).getByIndex(i).dim2.Shirts)) && ('Nike' in lc(this).getByIndex(i).dim3)) 
    || (('Nike' in lc(this).getByIndex(i).dim3) && ('Shirts' in lc(this).getByIndex(i).dim2)))

这里基本上有两个陈述:如果衬衫和耐克出现在耐克但根本没有类别,我希望这个陈述能够证明是真的。希望有所帮助,但我会将其转移到代码审查,但会等待一个可靠的答案,所以有人得到了信息:) ...感谢您的提示。

3 个答案:

答案 0 :(得分:2)

你可以这样做:

else {
  //comparisons
  var a=yadayadayada,
      b=blablabla,
      c=1024,
      d=document.getElementById('something').value;
      //and so on
  if( a===b || c<=d ){ //that should keep your if-clause a lot more readable!!
    //actions
  }
}

希望这有帮助!

更新(在TS更新后): 现在我们对你的数据有了更多的了解,你会问:'如果衬衫和耐克在场,或者Nike但没有任何类别,那就是真的,那么这就是你想要的:

else {
  var b='Nike', //brand
      c='Shirts', //category
      d=lc(this).getByIndex(i),
     d2=d.dim2,
     d3=d.dim3;
  if( ((b in d2) && d2[c]) || (b in d2) ){ 
    //actions
  }
}

答案 1 :(得分:2)

根据GitaarLAB的回答,这个怎么样?

...
else {
    var x      = lc(this).getByIndex(i),
        y      = x.dim2.Shirts,
        nike   = 'Nike'   in x.dim3,
        shirts = 'Shirts' in x.dim2;

    if (!y && nike || nike && shirts) { ... }
}

答案 2 :(得分:1)

既然你有“耐克......等等”。在两个语句中,您可以将其缩小为:

else if ( ('Nike' in lc(this).getByIndex(i).dim3)
  && (!lc(this).getByIndex(i).dim2.Shirts || ('Shirts' in lc(this).getByIndex(i).dim2)) )
相关问题