JavaScript 中多个 OR 运算符的替代方案
在本文中,我们将学习使用多个 || 的替代方法。 (或运算符)在条件语句中。条件语句中的多个 OR运算符可能会导致代码可读性差,并且通常在功能上受到限制。
可以重写以下 JavaScript 代码以以其他方式执行相同的任务:
javascript
let user = "motivated";
let recommended_org = "";
/* If user is a geek or motivated or
curious then the recommended organisation
for him/her is GeeksforGeeks */
if (user == "geek" ||
user == "motivated" ||
user == "curious") {
recommended_org = "GeeksforGeeks";
console.log(recommended_org);
}
javascript
/* If user is a geek or motivated or
curious then the recommended organisation
for him/her is GeeksforGeeks */
if (user == "geek") {
recommended_org = "GeeksforGeeks";
}
else if (user == "motivated") {
recommended_org = "GeeksforGeeks";
}
else if (user == "curious") {
recommended_org = "GeeksforGeeks";
}
else {
recommended_org = "No recommendation"
}
console.log(recommended_org);
javascript
let user = "curious";
let recommended_org = "No recommendation";
/* If user is a geek or motivated or
curious then the recommended organisation
for him/her is GeeksforGeeks */
switch (user) {
case "geek":
case "motivated":
case "curious":
recommended_org = "GeeksforGeeks"
break;
default:
recommended_org = "No Recommendation"
}
console.log(recommended_org);
javascript
let user = "geek";
/* A recommended_org map object which consists
the behaviour of the user as the key and the
recommended organisation as the values */
var recommended_org = {
"geek": "GeeksforGeeks",
"motivated": "GeeksforGeeks",
"curious": "GeeksforGeeks",
"other": "No Recommendation"
}
/* if user is a geek or motivated or
curious then the recommended organisation
for him/her is GeeksforGeeks */
console.log(recommended_org[user] ||
recommended_org["other"]);
输出:
GeeksforGeeks
方法 1:这种方法使用 if-else 阶梯来处理所有不同的可能性。
在这种方法中,语法大大增加。由于每个 if 语句的结果是相同的,因此最好使用使用多个 OR 运算的原始方法。
但是,随着程序的选项变得越来越复杂,if-else 梯形图是首选,因为它增加了代码的可读性。它也非常灵活,可以添加任意数量的条件甚至嵌套条件,由于这些原因,它可以用于复杂的条件。
下面的示例演示了这种方法:
javascript
/* If user is a geek or motivated or
curious then the recommended organisation
for him/her is GeeksforGeeks */
if (user == "geek") {
recommended_org = "GeeksforGeeks";
}
else if (user == "motivated") {
recommended_org = "GeeksforGeeks";
}
else if (user == "curious") {
recommended_org = "GeeksforGeeks";
}
else {
recommended_org = "No recommendation"
}
console.log(recommended_org);
输出:
GeeksforGeeks
方法 2:这种方法使用 switch-case 语句来处理所有不同的可能性。
Switch-case 语句功能强大,它们增加了代码的可读性,并且使用 default 和 break 语句使代码编写效率更高。
在这种情况下,通过使用 switch-case 语句增加了语法,但与以前的方法相比,代码的重复更少。在 if-else 阶梯中,我们使用了 3 次赋值语句,但在这种方法中,我们使用了 1 次。但是嵌套 switch case 会使代码变得非常复杂,并且对于具有大量嵌套条件的问题通常不优选,因此它不像 if-else 那样灵活。
下面的示例演示了这种方法:
javascript
let user = "curious";
let recommended_org = "No recommendation";
/* If user is a geek or motivated or
curious then the recommended organisation
for him/her is GeeksforGeeks */
switch (user) {
case "geek":
case "motivated":
case "curious":
recommended_org = "GeeksforGeeks"
break;
default:
recommended_org = "No Recommendation"
}
console.log(recommended_org);
输出:
GeeksforGeeks
方法 3:这种方法使用地图对象来处理所有不同的可能性。
这种方法像 if-else 阶梯一样非常灵活,并且易于扩展,因为需要添加更多的键值对,但是在非常复杂的示例中它仍然不能替代上述方法。
下面的示例演示了这种方法:
javascript
let user = "geek";
/* A recommended_org map object which consists
the behaviour of the user as the key and the
recommended organisation as the values */
var recommended_org = {
"geek": "GeeksforGeeks",
"motivated": "GeeksforGeeks",
"curious": "GeeksforGeeks",
"other": "No Recommendation"
}
/* if user is a geek or motivated or
curious then the recommended organisation
for him/her is GeeksforGeeks */
console.log(recommended_org[user] ||
recommended_org["other"]);
输出:
GeeksforGeeks
任何替代方案都不能被称为优越,因为它们都有一些优点和缺点。这取决于程序员的个人意见和使用它的上下文。但是,了解可用的替代方案仍然是一个好习惯。