博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript复选框_JavaScript可选链接
阅读量:2503 次
发布时间:2019-05-11

本文共 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.

如果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 .

您现在可以使用使用此语法。

翻译自:

javascript复选框

转载地址:http://jvqgb.baihongyu.com/

你可能感兴趣的文章
[学习笔记]后缀系列总结
查看>>
UIActionSheet 取消按钮不可点击解决方法
查看>>
PHP中的__set和__get方法
查看>>
ruby对象模型
查看>>
Java 正则表达式
查看>>
性能瓶颈定位整体思路
查看>>
十道海量数据处理面试题与十个方法大总结
查看>>
Archlinux GNOME 3 美化
查看>>
20189320《网络攻防》第五周作业
查看>>
2019.1.18笔记
查看>>
删除了Ubuntu之后,不能正常进入到Win7系统之中的解决办法
查看>>
写一个正则表达式匹配手机号
查看>>
Linux试题
查看>>
TableLock插件
查看>>
java 获取页面中的 a 标签 的 href 实例
查看>>
Knowledge Point 20180305 详解精度问题
查看>>
开发 Windows 8 Bing地图应用(4)
查看>>
mysql-python安装时mysql_config not found
查看>>
loadrunner 场景设计-添加Unix、Linux Resources计数器
查看>>
Python 基于python编写一些算法程序等
查看>>