javascript复选框_JavaScript可选链接

javascript复选框

The optional chaining operator is a new feature coming in the next ECMAScript standard.

可选的链接运算符是下一个ECMAScript标准中的一项新功能。

It’s still not official, but available in Chrome 80 behind a flag.

它仍然不是官方的,但在带有标志的Chrome 80中可用

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 = null
const 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?.color
const colorName = car?.color?.name

If car is null or undefined, the result will be undefined.

如果carnullundefined ,则结果将为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 this Babel plugin.

您现在可以使用此Babel插件使用此语法。

翻译自: https://flaviocopes.com/javascript-optional-chaining/

javascript复选框

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值