short hand for chaining logical operators in javascript?

后端 未结 7 1375
眼角桃花
眼角桃花 2021-01-12 03:13

Is there a better way to write the following conditional in javascript?

if ( value == 1 || value == 16 || value == -500 || value == 42.42 || value == \'somet         


        
7条回答
  •  -上瘾入骨i
    2021-01-12 03:31

    You could extend the array object:

    Array.prototype.contains = function(obj) {
      var i = this.length;
      while (i--) {
        if (this[i] == obj) {
          return true;
        }
      }
      return false;
    }
    

    Then if you store all those values in an array you could do something like MyValues.contains(value)

提交回复
热议问题