Javascript 带有逻辑和的打字稿开关盒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42612173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:17:01  来源:igfitidea点击:

Typescript switch case with logical and

javascripttypescriptswitch-statement

提问by Thinker

I am trying to write a switch statement but it doesn't seem to work how I want.

我正在尝试编写一个 switch 语句,但它似乎并不像我想要的那样工作。

getExerciseDescription(exerciseId, intensity_level){

    alert(exerciseId + " " + intensity_level)

    switch (exerciseId && intensity_level) {
        case (1 && 1):
        this.header="Exercise 1 Level 1";
        this.instructions="Exercise 1 Level 1";
        break;
        case (1 && 2):
        this.header="Exercise 1 Level 2";
        this.instructions="Exercise 1 Level 2";
        break;  


        case (2 && 1):
        this.header="Exercise 2 Level 1";
        this.instructions="Exercise 2 Level 1";
        break;  
        case (2 && 2):
        this.header="Exercise 2 Level 2";
        this.instructions="Exercise 2 Level 2";
        break;

        default:
        this.header="Default";
        this.instructions="Default";
        break;
    }

    return new Popup(this.header, this.instructions);
} 

The alerts gives 2 and 1 but the returned value is for (1 && 1). Why is it so? How can I fix this?

警报给出 2 和 1,但返回的值是 (1 && 1)。为什么会这样?我怎样才能解决这个问题?

回答by Charles Clayton

You just can't use a switchlike that. (1 && 1) == (2 && 1) == 1and (1 && 2) == (2 && 2) == 2, so you're doing the equivalent of:

你只是不能使用switch这样的。(1 && 1) == (2 && 1) == 1(1 && 2) == (2 && 2) == 2,所以你做的相当于:

getExerciseDescription(exerciseId, intensity_level){

    alert(exerciseId + " " + intensity_level)

    switch (exerciseId && intensity_level) {
        case (1):
        this.header="Exercise 1 Level 1";
        this.instructions="Exercise 1 Level 1";
        break;
        case (2):
        this.header="Exercise 1 Level 2";
        this.instructions="Exercise 1 Level 2";
        break;  


        case (1):
        this.header="Exercise 2 Level 1";
        this.instructions="Exercise 2 Level 1";
        break;  
        case (2):
        this.header="Exercise 2 Level 2";
        this.instructions="Exercise 2 Level 2";
        break;

        default:
        this.header="Default";
        this.instructions="Default";
        break;
    }

    return new Popup(this.header, this.instructions);
} 

So of course the lower two cases will never execute. You're better of just using ifand else ifstatements, or maybe nested switches if you want.

所以当然下面的两种情况永远不会执行。您最好只使用ifandelse if语句,或者如果需要,也可以使用嵌套开关。

You could also do something like:

您还可以执行以下操作:

switch (exerciseId + " " + intensity_level) {
    case("1 1"): ...
    case("1 2"): ...
    case("2 1"): ...
    case("2 2"): ...

回答by Christopher Moore

That isn't how a switchstatement will evaluate. For your scenario it will always evaluate to the 2nd integer in the logical and &&.

这不是一个switch声明将如何评估。对于您的场景,它将始终评估为逻辑和中的第二个整数&&

(More information about Logical Operators)

(有关逻辑运算符的更多信息)

Logical AND (&&)

expr1 && expr2

Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

逻辑与 (&&)

expr1 && expr2

如果可以转换为 false,则返回 expr1;否则,返回 expr2。因此,当与布尔值一起使用时,如果两个操作数都为真,&& 返回真;否则,返回 false。

You actually don't even need to use a switch, you can write it with a simple if

你实际上甚至不需要使用开关,你可以用一个简单的 if

if (exerciseId <= 2 && intensity_level <= 2){
    this.header=`Exercise ${exerciseId} Level ${intensity_level}`;
    this.instructions=`Exercise ${exerciseId} Level ${intensity_level}`;
} else {
    this.header="Default";
    this.instructions="Default";
}

回答by Babar Bilal

The logical operators &&, ||and !always return trueor falseso the possible switch cases are only true and false. You should go with strings or numbers cases only.

逻辑运算符&&||并且!总是返回truefalse使可能的开关情况是只有真假。您应该只使用字符串或数字案例。