本文共 1489 字,大约阅读时间需要 4 分钟。
javascript复选框
The optional chaining operator is a new feature coming in the next ECMAScript standard.
可选的链接运算符是下一个ECMAScript标准中的一项新功能。
It’s still not official, but behind a flag.
它仍然不是官方的,但在带有标志的 。
Have you ever used the && operator as a fallback? It’s one of my favorite JavaScript features.
您曾经使用&&运算符作为后备吗? 这是我最喜欢JavaScript功能之一。
In JavaScript, you can first check if an object exists, and then try to get one of its properties, like this:
在JavaScript中,您可以先检查对象是否存在,然后尝试获取其属性之一,如下所示:
const car = nullconst color = car && car.color
Even if car
is null, you don’t have errors and color
is assigned the null
value.
即使car
为空,也不会出现错误,并且为color
分配了null
值。
You can go down multiple levels:
您可以分为多个级别:
const car = {}const colorName = car && car.color && car.color.name
In some other languages, using &&
might give you true or false, since it’s usually a logic operator.
在其他一些语言中,使用&&
可能会给您带来真假,因为它通常是逻辑运算符。
Not in JavaScript, and it allows us to do some cool things.
不在JavaScript中,它使我们能够做一些很棒的事情。
Now this new optional chaining operator will let us be even more fancy:
现在,这个新的可选链接运算符将使我们更加喜欢:
const color = car?.colorconst colorName = car?.color?.name
If car
is null
or undefined
, the result will be undefined
.
如果car
为null
或undefined
,则结果将为undefined
。
With no errors (while with && in case car
was undefined
we had a ReferenceError: car is not defined
error)
没有错误(在使用&&的情况下,如果car
undefined
我们会遇到ReferenceError: car is not defined
错误)
You can use this syntax today using .
您现在可以使用使用此语法。
翻译自:
javascript复选框
转载地址:http://jvqgb.baihongyu.com/