如何在JavaScript中解释这种条件分支


this.orientation = this.props.orientation
? this.props.orientation == 'horizontal'
? 'row'
: 'column'
: 'column';

我可以知道我如何解释这个条件分支吗?这个代码是从网上其他地方得到的。我确实读过这个https://javascript.info/ifelse#multiple但我还是听不懂。

根据我的理解,这个.props.orientation的方向是从父对象中检索的。如果this.props.rietation=='horizontal',那么返回列?否则,如果this.props.orientation=="row",则返回列?

插入括号会让事情变得更清楚。

this.orientation = this.props.orientation ?
(this.props.orientation == 'horizontal' ? 'row' : 'column') :
'column';

如果this.props.orientation不为null或为空,则根据其与"0"的比较值返回'row''column';"水平";(如果为true则为'row',否则为'column'(。如果为null或为空,则默认返回'column'

最新更新