博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript空合并
阅读量:2504 次
发布时间:2019-05-11

本文共 1267 字,大约阅读时间需要 4 分钟。

A new JavaScript feature that’s going to be widely used, soon, is the nullish coalescing operator ??.

即将被广泛使用的新JavaScript功能是无效的合并运算符??

What’s that? ?? ?

那是什么? ??

Have you ever used || to set a default value if a variable was null or undefined?

您曾经使用过|| 如果变量为null或未定义,设置默认值吗?

For example, like this:

例如,像这样:

const myColor = color || 'red'

Well, nullish coalescing is going to replace || in there:

好吧,无效的合并将替换|| 在那里:

const myColor = color ?? 'red'

Why?

为什么?

Well, there is a whole range of bugs that hide underneath the surface when using || to provide a fallback value.

好吧,当使用||时,有很多错误隐藏在表面之下。 提供后备值。

In short, || handles values as falsy. ?? handles values as nullish (hence the name).

简而言之, || 将值视为虚假。 ?? 将值处理为空值(因此命名)。

Which means that with || the second operand is evaluated if the first operand is undefined, null, false, 0, NaN or ''.

这意味着|| 如果第一个操作数是undefinednullfalse0NaN'' ,则计算第二个操作数。

?? on the other hand limits this list to only undefined and null.

?? 另一方面,将此列表限制为只有undefinednull

Which might suit your use case better!

哪个可能更适合您的用例!

The blog has a much more detailed post on this operator, but you got the gist here.

博客上有关于此运算符的更详细的文章,但是您在这里找到了要点。

It’s currently available in Chrome Canary, and in !

目前可在Chrome Canary和 !

Check my

查看我的

翻译自:

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

你可能感兴趣的文章
kafka web端管理工具 kafka-manager【转发】
查看>>
获取控制台窗口句柄GetConsoleWindow
查看>>
Linux下Qt+CUDA调试并运行
查看>>
51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
查看>>
OKMX6Q在ltib生成的rootfs基础上制作带QT库的根文件系统
查看>>
zabbix
查看>>
多线程基础
查看>>
完美解决 error C2220: warning treated as error - no ‘object’ file generated
查看>>
使用SQL*PLUS,构建完美excel或html输出
查看>>
SQL Server数据库笔记
查看>>
X-Forwarded-For伪造及防御
查看>>
android系统平台显示驱动开发简要:LCD驱动调试篇『四』
查看>>
Android 高仿微信头像截取 打造不一样的自定义控件
查看>>
Jenkins的初级应用(1)-Publish Over SSH
查看>>
利用正则表达式群发定制邮件
查看>>
【原】RDD专题
查看>>
第三周——构建一个简单的Linux系统MenuOS
查看>>
Docker 的两类存储资源 - 每天5分钟玩转 Docker 容器技术(38)
查看>>
Codeforces 257D
查看>>
常用的20个强大的 Sublime Text 插件
查看>>