【问题标题】:JavaScript tell which condition was hitJavaScript 告诉你命中了哪个条件
【发布时间】:2015-12-29 01:09:07
【问题描述】:

如果我在 JavaScript 中有这样的语句:

if(!me.a || !me.b || !me.c) {
     // I want to know which not was matched here
}

我如何知道在范围内匹配了哪个条件?有没有魔算子?

【问题讨论】:

    标签: javascript scope conditional


    【解决方案1】:

    嗯,一种方法是 && 每个条件的标识符并检查结果。

    var result = (!me.a && 1) || (!me.b && 2) || (!me.c && 3);
    
    if(result) {
        // if result is 1, then !me.a is true
        // if result is 2, then !me.b is true and !me.a is false
        // if result is 3, only !me.c is true
    }
    

    【讨论】:

    • 这就是所谓的应用你所知道的:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多