我知道您可以通过执行var variable = (condition) ? (true block) : (else block)
来设置带有一行 if/else 语句的变量,但我想知道是否有办法将 else if 语句放在那里。任何建议将不胜感激,谢谢大家!
当然,你可以做嵌套的三元运算符,但它们很难阅读。
var variable = (condition) ? (true block) : ((condition2) ? (true block2) : (else block2))
tl;dr
是的,你可以... 如果 a 然后 a,否则如果 b 那么如果 c 然后 c(b),否则 b,否则 null
a ? a : (b ? (c ? c(b) : b) : null)
a
? a
: b
? c
? c(b)
: b
: null
加长版本
三元运算符?:
用作内联 if-else 是正确的关联。简而言之,这意味着最右边的?
首先被馈送,它正好需要一个最接近的操作数在左边
:
。实际上,请考虑以下语句(与上述相同):
a ? a : b ? c ? c(b) : b : null
最右边的?
首先被喂饱,所以找到它和它周围的三个参数,然后连续向左扩展到另一个?
。
a ? a : b ? c ? c(b) : b : null
^ <---- RTL
1. |1-?-2----:-3|
^ <-
2. |1-?|--2---------|:-3---|
^ <-
3.|1-?-2-:|--3--------------------|
result: a ? a : (b ? (c ? c(b) : b) : null)
这是计算机读取它的方式:
- 术语
a
是 read.
节点:a
- 非终端
?
被读取.
节点:a ?
- 术语
a
是 read.
节点:a ? a
- 读取非终端
:
.
节点:a ? a :
- 术语
b
为 read.
节点:a ? a : b
- 读取非终端
?
,触发右关联性规则。关联性决定:
节点:a ? a : (b ?
- 术语
c
是 read.
节点:a ? a : (b ? c
- 读取非终端
?
,重新应用右关联性规则.
节点:a ? a : (b ? (c ?
- 读取术语
c(b)
.
节点:a ? a : (b ? (c ? c(b)
- 非终端
:
被读取.
节点:a ? a : (b ? (c ? c(b) :
- 术语
b
是 read.
节点:a ? a : (b ? (c ? c(b) : b
- 读取非终端
:
。满足先前作用域中的三元运算符?:
,并且作用域已关闭.
节点:a ? a : (b ? (c ? c(b) : b) :
- 术语
null
读取.
节点:a ? a : (b ? (c ? c(b) : b) : null
- 没有要读取的令牌。关闭剩余的左括号.
#Result 是:a ? a : (b ? (c ? c(b) : b) : null)
更好的可读性
上面丑陋的单行可以(并且应该)重写为:
(请注意,缩进不会像方括号 () 那样隐式定义正确的闭包。
a
? a
: b
? c
? c(b)
: b
: null
例如
return a + some_lengthy_variable_name > another_variable
? "yep"
: "nop"
更多阅读
Mozilla: JavaScript Conditional Operator
维基:运算符关联性
奖励:逻辑运算符
var a = 0 // 1
var b = 20
var c = null // x=> {console.log('b is', x); return true} // return true here!
a
&& a
|| b
&& c
&& c(b) // if this returns false, || b is processed
|| b
|| null
像这个例子一样使用逻辑运算符是丑陋和错误的,但这就是它们闪耀的地方......
"零合并"
这种方法具有细微的局限性,如下面的链接中所述。有关正确的解决方案,请参阅 Bonus2 中的无效合并。
function f(mayBeNullOrFalsy) {
var cantBeNull = mayBeNullOrFalsy || 42 // "default" value
var alsoCantBe = mayBeNullOrFalsy ? mayBeNullOrFalsy : 42 // ugly...
..
}
短路评估
false && (anything) // is short-circuit evaluated to false.
true || (anything) // is short-circuit evaluated to true.
逻辑运算符
空合并
短路评估
奖励2:JS中的新功能
适当的"无效合并"
developer.mozilla.org~Nullish_coalescing_operator
function f(mayBeNullOrUndefined, another) {
var cantBeNullOrUndefined = mayBeNullOrUndefined ?? 42
another ??= 37 // nullish coalescing self-assignment
another = another ?? 37 // same effect
..
}
可选链接
第 4 阶段完成提案 https://github.com/tc39/proposal-optional-chaininghttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
// before
var street = user.address && user.address.street
// after
var street = user.address?.street
// combined with Nullish coalescing
// before
var street = user.address
? user.address.street
: "N/A"
// after
var street = user.address?.street ?? "N/A"
// arrays
obj.someArray?.[index]
// functions
obj.someMethod?.(args)
简单来说:
var x = (day == "yes") ? "Good Day!" : (day == "no") ? "Good Night!" : "";
这主要用于分配变量,它使用二项式条件,例如。
var time = Date().getHours(); // or something
var clockTime = time > 12 ? 'PM' : 'AM' ;
没有 ElseIf,为了开发而不使用链接,您可以使用switch
如果你有多个条件.js
这是一个古老的线程,但我想我会把我的两分钱放进去。三元运算符可以按以下方式嵌套:
var variable = conditionA ? valueA : (conditionB ? valueB: (conditionC ? valueC : valueD));
例:
var answer = value === 'foo' ? 1 :
(value === 'bar' ? 2 :
(value === 'foobar' ? 3 : 0));
if-else:
a = b ? (true block) : (false block)
if-else if-else:
a = b ? (true block) : b = c ? (true block) : (false block)
如果:
a = b && (true block)
if-else-if(nested):
a = b ? (true block) : b = c && (true block)
- 注意想这么多,只是实现并查看结果,其中 a、b 和 c 是可变的
您可以根据需要链接任意数量的条件。如果您这样做:
var x = (false)?("1true"):((true)?"2true":"2false");
你会得到x="2true"
所以它可以表示为:
var variable = (condition) ? (true block) : ((condition)?(true block):(false block))
a === "a" ? do something
: a === "b" ? do something
: do something
如果我使用这样的代码
const alpha = a ? a : b ? b : c
它会得到
将此嵌套的三元运算提取到一个独立的语句中。
从所以我建议使用这个
const alpha = a || b || c
它对我有用