[英]One line of if..else if.. else statement
I tried to write one line if..elseif..else
statement but it always goes in else if
. 我试图写一行
if..elseif..else
语句,但是else if
总是在else if
行中写。
var x = "192.168.1.1"; x = x == ("google.com") ? ("true google.com") : (("yahoo.com") ? ("true yahoo.com") : ("192.168.1.1")); console.log(x);
Is there something I am missing? 我有什么想念的吗? Why does it always go in
else if
? 为什么要总是将其放入
else if
?
You missed the x == (""yahoo.com"")
statement 您错过了
x == (""yahoo.com"")
语句
var x = "192.168.1.1"; x = (x == "google.com") ? "true google.com" : (x == "yahoo.com") ? "true yahoo.com" : "192.168.1.1"; // --------------------------------------------^^^^^^^^^^^^^^^^------------------------------------ console.log(x);
But it will be more readable with if - else if - else
statements. 但是,
if - else if - else
使用if - else if - else
语句,它将更具可读性。 Don't make your code concise if it will decrease the readability. 如果代码会降低可读性,请不要使其简洁。
This does not answer the question 这不能回答问题
Why does it always go in
else if
?为什么要总是将其放入
else if
?
but it could help with 但它可以帮助
Is there something I am missing?
我有什么想念的吗?
Yes, you mis some clarity for further use and a clear pattern, how to get for a given string another string. 是的,您误解了一些进一步使用的清晰性和清晰的模式,即如何为给定字符串获取另一个字符串。
You could use an object, which is easy to maintain for the keyed values. 您可以使用一个对象,该对象易于维护键值。
values = { "google.com": "true google.com", "yahoo.com": "true yahoo.com", default : "192.168.1.1" };
The call works with a default operator ||
该调用与默认运算符
||
(logical OR): (逻辑或):
x = values[x] || values.default;
var x = "192.168.1.1", values = { "google.com": "true google.com", "yahoo.com": "true yahoo.com", default : "192.168.1.1" }; x = values[x] || values.default; console.log(x);
Your ternary operation 您的三元运算
x = x == ("google.com") ? ("true google.com") : (("yahoo.com") ? ("true yahoo.com") : ("192.168.1.1"));
can be thought of as if-else if-else
block as follows: 可以认为是
if-else if-else
块,如下所示:
if(x == ("google.com")) {
x = "true google.com";
}
else {
if("yahoo.com") {
x = "true yahoo.com"; //Always true since it is a non-empty string
}
else {
x = "192.168.1.1"
}
}
So since you are initializing x to "192.168.1.1", it obviously is not equal to the the string specified ("google.com") in the first condition ( if
block). 因此,由于您将x初始化为“ 192.168.1.1”,因此它显然不等于第一个条件(
if
block)中指定的字符串(“ google.com”)。 So it moves on to the else block and evaluates the if
condition inside the else
block. 因此,它进入else块并评估
else
块内的if
条件。 This if
block in turn only checks if a string literal "yahoo.com" is empty or not. 反过来,此
if
块仅检查字符串文字“ yahoo.com”是否为空。 Since it is not empty, the condition is satisfied. 由于不为空,因此满足条件。
For your purpose, you need to change that from if("yahoo.com")
to x == if("yahoo.com")
. 为了您的目的,您需要将其从
if("yahoo.com")
更改为x == if("yahoo.com")
。 However, once you make this change, it will always go to the else block because the first two conditions will never satisfy. 但是,一旦进行了更改,它将始终转到else块,因为前两个条件将永远无法满足。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.