【问题标题】:Not sure I understand this nested ternary operation不确定我是否理解这个嵌套的三元运算
【发布时间】:2020-12-28 21:26:50
【问题描述】:

可以使用一些帮助理解(或帮助进行更正),以及关于您如何知道的解释或链接,以便当我再次遇到它们时可以弄清楚这些。我发现的所有教程都没有太大帮助。这是操作。括号表示一个时间序列(数组):

a = b==1 ? c ? d : e > a[1] ? e : a[1] : c ? d : a[1]

翻译过来就是:

a = if b==1 and c then d 
    else if e > a[1] then e 
    else a[1] 
    else if c then d
    else a[1]

...对吗?如果正确,我不明白 else 怎么会出现在两个 else-if 之间。链条不会在第一个 else 之后停止评估吗?我认为 else 本质上的意思是“否则”。

【问题讨论】:

    标签: conditional-operator


    【解决方案1】:

    我相信执行将等同于这个而不是b == 1 and c

    function test() {
        if (b==1) {
            if (c) {
                return d;
            } else {
                if (e > a[1]) {
                    return e;
                } else {
                    return a[1];
                }
            }
        } else {
            if (c) {
                return d;
            } else {
                return a[1];
            }
        }
    }
    
    a = test();
    

    【讨论】:

    • 谢谢。您知道的任何教程可以在将来提供帮助吗?
    • @sgjohnson1981 这是一个可以将三元表达式转换为if-else的网站:converter.website-dev.eu
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 2011-10-29
    • 2021-05-27
    相关资源
    最近更新 更多