我试着写一个行if..elseif..else语句,但总是进去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);Run Code Online (Sandbox Code Playgroud)
有什么我想念的吗?它为什么总是进去else if?
你错过了这个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);Run Code Online (Sandbox Code Playgroud)
但是if - else if - else语句更具可读性.如果它会降低可读性,请不要使代码简洁.
| 归档时间: |
|
| 查看次数: |
94 次 |
| 最近记录: |