js中的三元运算

时间:2018-04-18 06:23:41

标签: javascript ternary

也许我不了解三元操作但是

如果我是对的

    test ? true : false

所以这应该给出

function toto(x, y)
{ 
    return (x > 0 ? x < 7 ? true : false : false &&
                y > 0 ? y < 6 ? true : false : false)
}

仅当0时为真

但如果我这样做

toto(4,6)

它返回true,为什么?我错过了什么?

4 个答案:

答案 0 :(得分:1)

您需要eslint格式化代码,这是格式化的代码,请参阅:

function toto(x, y) {
  return x > 0
    ? x < 7
      ? true
      : false
    : false && y > 0
      ? y < 6
        ? true
        : false
      : false
}

图像:

enter image description here 我认为,它更容易理解

答案 1 :(得分:1)

就这样做:

function toto(x, y)
{ 
    return (x > 0 ? x < 7 ? true : false : false ) &&
                ( y > 0 ? y < 6 ? true : false : false)
}

使用exp1和exp2之前和之后的括号 是的,它有点不可读^^

编辑:我也会这样做

return (x > 0 && x < 7) && (y > 0 && y < 6)

答案 2 :(得分:0)

你不是想要实现这个目标吗? chceking ParentForm是否从0..7和Property value is not valid. Details: Object reference not set to an instance of an object.是0..6?

UserControl

答案 3 :(得分:0)

影响它的运营商优先级

function toto(x, y)
{ 
return ((x > 0 ? x < 7 ? true : false : false) && (y > 0 ? y < 6 ? true : false : false)) 
}