Javascript:如何简化具有多个OR条件的if语句? [英] Javascript: How can I simplify an if-statement with multiple OR conditions?

查看:102
本文介绍了Javascript:如何简化具有多个OR条件的if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果我在写这篇文章时犯了错误。我是新来的,我不知道这是怎么回事,希望我能快速学习。我也是JavaScript的新手。

Sorry if I've made mistakes writing this post. I'm new here and I don't know how this works, hope I learn quick. I am also new at JavaScript.

所以问题是:我的 code.elements.js 文件中有这个,我不能让它起作用。

So the question is: I have this on my code.elements.js file and I can't make it work.

这样做有用吗?

if (codePrompt == (codeSwitch || codeSwitchBG || codeBlur || codeShowInfo)){};

或者我必须按正常方式制作吗?比如

Or do I have to make it by the normal way?, like

if (codePrompt == codeSwitch || codePrompt == codeSwitchBG || codePrompt == codeBlur || codePrompt == codeShowInfo){};


var codeSwitch = 'switch()';
var codeSwitchBG = 'switch(background)';
var codeBlur = 'blur()';
var codeShowInfo = 'showInfo()';

$(".code").on("click", function () {
var codePrompt = prompt("Set the code in the command line.");
if (codePrompt == (codeSwitch || codeSwitchBG || codeBlur || codeShowInfo)) {
    if (codePrompt == codeSwitch) {
        alert("Switching background...");
        console.log("Used '" + codeSwitch + "' command.");
    } else if(codePrompt == codeBlur) {
        alert("Blurring elements...");
        console.log("Used '" + codeBlur + "' command.");
    } else if(codePrompt == codeSwitchBG) {
        alert("Switching background...");
        console.log("Used '"+ codeSwitchBG + "' command.");
    }else if(codePrompt == codeShowInfo) {
        alert("Showing info...");
        console.log("Used '"+ codeShowInfo + "' command.");
    }
} else {
    alert("Wrong command, try again.");
    console.log("Wrong command, try again.");
};
});


推荐答案

因为JavaScript有短路评估,你的字符串是 truthy 然后你需要使用第二种方法或你所说的正常方式。

Because JavaScript has short circuit evaluation and your strings are truthy then you need to use the second approach or what you referred to as "the normal way".

第一种方法不起作用,因为你最终评估错误的东西。评估的工作方式如下:

The first way does not work because you end up evaluating the wrong thing. Evaluation works like this:

var result = 0 || 零;


  • 0 已评估,决心是假的。

  • 被评估为真实并成为结果。

  • 0 is evaluated and determined to be falsy.
  • "zero" is evaluated as truthy and becomes the result.

var result =zero|| 0;


  • 评估零决定是真实的,并返回结果。

  • 0 未评估,因为短路评估。

  • "zero" is evaluated and determined to be truthy and returned as the result.
  • 0 is not evaluated because short circuit evaluation.

在原始代码中:

if(codePrompt ==(codeSwitch || codeSwitchBG || codeBlur || codeShowInfo)) {};

|| 的运算符关联性是从左到右。括号从内到外进行评估。

The operator associativity of || is left to right. Parenthesis are evaluated inner to outer.

(codeSwitch || codeSwitchBG || codeBlur || codeShowInfo)首先进行评估。由于我们已经讨论过的规则,结果变为 codeSwitch

(codeSwitch || codeSwitchBG || codeBlur || codeShowInfo) is evaluated first. Because of the rules we already discussed the result becomes codeSwitch:


  • codeSwitch || codeSwitchBG成为codeSwitch

  • codeSwitch || codeBlur成为codeSwitch

  • codeSwitch || codeShowInfo成为codeSwitch

所以你最终评估:

if(codePrompt == codeSwitch)

当然这是错误的。

这篇关于Javascript:如何简化具有多个OR条件的if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆