您可以像这样将&& 运算符与ternary operator 组合起来:
(a && b && c && d > 0) ? true : false // will return true if all integers are more than 0
jsFiddle: https://jsfiddle.net/AndrewL64/6bk1bs0w/
或者您可以将变量分配给数组并像这样使用Array.prototype.every():
let x = [a, b, c, d]
x.every(i => i > 0) // will return true if all integers are more than 0
jsFiddle: https://jsfiddle.net/AndrewL64/6bk1bs0w/1/
或者为了使上面的内容更短,你可以直接将值放在一个数组中,然后像这样直接在数组上使用every:
[0, 1, 0, 0].every(i => i > 0); // will return false since all integers are not more than 0
jsFiddle: https://jsfiddle.net/AndrewL64/6bk1bs0w/3/
或者你可以创建一个可重复使用的函数,然后用一行代码多次运行它:
function moreThanOne(...args){
// Insert any of the above approaches here but reference the variables/array with the word 'arg'
}
moreThanOne(3,1,2,0); // will return false as well as alert false
moreThanOne(3,1,2,4); // will return true as well as alert true
jsFiddle: https://jsfiddle.net/AndrewL64/6bk1bs0w/2/