Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

scope - JavaScript tell which condition was hit

If I have a statement in JavaScript like:

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

How do I know which conditional was matched inside the scope? Is there any magic operator?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, one way is to && an identifier with each condition and check the result.

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...