ES11:nullish coalescing operator, 你真的会用吗?

阅读时长 5 分钟读完

介绍

在 JavaScript 中,我们经常要处理 null 或 undefined 值。在 ES11 中,新增了一种操作符,叫做 nullish coalescing operator(空值合并操作符),用来处理 null 或 undefined 值的问题。

以往,我们经常使用 || 操作符或者三元运算符来判断一个变量是否为 null 或 undefined 值。但是这些方式存在一些问题,在某些情况下会得到意想不到的结果。

在这篇文章中,我们将深入探讨 nullish coalescing operator 的用法,帮助你更好地使用它来处理 null 或 undefined 值。

使用方法

在 ES11 中,nullish coalescing operator 的符号为 ??。它的用法类似于三元运算符,但是更加简洁。

-- -------------------- ---- -------
----- --------- - -----
----- --------- - ----------
----- --------- - --
----- --------- - ---------
----- --------- - ---

--------------------- -- -------- -------- -- ------- -----
--------------------- -- -------- -------- -- ------- -----
--------------------- -- -------- -------- -- -
--------------------- -- -------- -------- -- ------
--------------------- -- -------- -------- -- --
展开代码

在上面的代码中,我们使用了 nullish coalescing operator 来处理不同的变量。如果变量的值为 null 或 undefined,它就会输出默认值,否则输出变量的值。

值得注意的是,nullish coalescing operator 仅在左侧的值为 null 或 undefined 时才会输出默认值,否则会输出变量的值,与 || 操作符不同。

案例分析

接下来,我们将通过一个实际的案例来深入探讨 nullish coalescing operator 的用法。

假设我们正在构建一个网站,我们需要获取用户的个人信息。我们的代码如下:

-- -------------------- ---- -------
----- -------- - -
  ----- ---
  ---- -----
  ------ ----------------------
  -------- -
    ------- ---
    ----- ---
    ------ ---
    -------- -----
  --
--

----- ---- - ------------- - ------------- - ----------
----- --- - ------------ - ------------ - ---
----- ----- - -------------- - -------------- - ----------
----- ------ - ----------------------- - ----------------------- - ----------
----- ---- - --------------------- - --------------------- - ----------
----- ----- - ---------------------- - ---------------------- - ----------
----- ------- - ------------------------ - ------------------------ - ------

----------------- ---- ------ ------- ----- ------ ---------
展开代码

在上面的代码中,我们使用了三元运算符来判断每个变量是否为 null 或 undefined,如果是就输出默认值,否则输出变量的值。但是这种写法存在一些问题。

例如,如果 userInfo 中的属性值为 0 或 "",那么上面的代码将会将它们都视为 null 或 undefined,输出默认值。而这并不是我们想要的结果。

为了解决这个问题,我们可以使用 nullish coalescing operator 来处理变量信息。

-- -------------------- ---- -------
----- ---- - ------------- -- ----------
----- --- - ------------ -- ---
----- ----- - -------------- -- ----------
----- ------ - ----------------------- -- ----------
----- ---- - --------------------- -- ----------
----- ----- - ---------------------- -- ----------
----- ------- - ------------------------ -- ------

----------------- ---- ------ ------- ----- ------ ---------
展开代码

在上面的代码中,我们使用了 nullish coalescing operator 来处理每个变量信息。如果变量的值为 null 或 undefined,它就会输出默认值,否则输出变量的值。这种写法可以避免将 0 和 "" 也视为 null 或 undefined,输出错误的结果。

结论

nullish coalescing operator 是一个非常实用的操作符,它可以避免将 0 和 "" 也视为 null 或 undefined,输出错误的结果。在日常开发中,我们可以更加方便地使用它来处理 null 或 undefined 值。

但是需要注意的是,nullish coalescing operator 只能处理 null 或 undefined 值,如果是其它值的话就无法处理。因此,在实际开发中需要结合实际情况来选择正确的处理方式。

来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/676f70ffe9a7045d0d72eddf

纠错
反馈

纠错反馈