我遇到了以下代码:

      if (proto.hasOwnProperty(name)) {
         !(specPolicy === SpecPolicy.DEFINE_MANY || specPolicy === SpecPolicy.DEFINE_MANY_MERGED) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactClassInterface: You are attempting to define ' + '`%s` on your component more than once. This conflict may be due ' + 'to a mixin.', name) : invariant(false) : undefined;
      }


您如何阅读第2行中的三元运算符?

最佳答案

尝试格式化代码缩进以更好地理解。

if (proto.hasOwnProperty(name)) {
    !(
        specPolicy === SpecPolicy.DEFINE_MANY //1
        ||
        specPolicy === SpecPolicy.DEFINE_MANY_MERGED //1
    ) ?
        process.env.NODE_ENV !== 'production' ? //2
            invariant(false, 'ReactClassInterface: You are attempting to define ' + '`%s` on your component more than once. This conflict may be due ' + 'to a mixin.', name) :
            invariant(false)//3
        : undefined;//4
}


(1)如果specPolicy既不是SpecPolicy.DEFINE_MANY也不是SpecPolicy.DEFINE_MANY_MERGED,则检查process.env.NODE_ENV是否不是“生产”。
(2)如果是,则调用invariant(false, 'Re....,否则调用(3)invariant(false)
如果(1)为true,则返回undefined(4)。

10-04 15:58