为什么我喜欢JavaScript的Optional Chaining

开发 前端
下面让我们来看看 optional chaining 是如何通过在深度访问可能缺少的属性时删除样板条件和变量来简化代码的。

 [[275113]]

JavaScript 的特性极大地改变了你的编码方式。从 ES2015 开始,对我代码影响最多的功能是解构、箭头函数、类和模块系统。

截至 2019 年 8 月,一项新提案 optional chaining 达到了第3阶段,这将是一个很好的改进。Optional Chaining 改变了从深层对象结构访问属性的方式。

下面让我们来看看 optional chaining 是如何通过在深度访问可能缺少的属性时删除样板条件和变量来简化代码的。

1. 问题

由于 JavaScript 的动态特性,对象可以有区别很大的嵌套对象结构。

通常,你在以下情况下处理此类对象:

  •  获取远程 JSON 数据
  •  使用配置对象
  •  具有 optional 属性

虽然这为对象提供了支持不同结构数据的灵活性,但是在访问这些对象的属性时会增加复杂性。

bigObject 在运行时可以有不同的属性集: 

  1. // One version of bigObject  
  2. const bigObject = {  
  3.   // ...  
  4.   prop1: {  
  5.     //...  
  6.     prop2: {  
  7.       // ...  
  8.       value: 'Some value'  
  9.     }  
  10.   }  
  11. };  
  12. // Other version of bigObject  
  13. const bigObject = {  
  14.   // ...  
  15.   prop1: {  
  16.     // Nothing here     
  17.   }  
  18. }; 

因此,你必须手动检查属性是否存在: 

  1. // Later  
  2. if (bigObject &&   
  3.     bigObject.prop1 != null &&   
  4.     bigObject.prop1.prop2 != null) {  
  5.   let result = bigObject.prop1.prop2.value;  

这会产生很多样板代码。如果不需要写这些代码那就太好了。

让我们看看 optional chaining 如何解决这个问题,并减少样板条件。

2. 轻松的深入访问属性

让我们设计一个保存电影信息的对象。该对象包含一个 title 属性,以及可选的 director 和 actors。

movieSmall 对象只包含 title,而 movieFull 包含完整的属性集: 

  1. const movieSmall = {  
  2.   title: 'Heat'  
  3. };  
  4. const movieFull = {  
  5.   title: 'Blade Runner',  
  6.   director: { name: 'Ridley Scott' },  
  7.   actors: [{ name: 'Harrison Ford' }, { name: 'Rutger Hauer' }]  
  8. }; 

让我们写一个获取导演名字的函数。请记住,director 属性可能会不存在: 

  1. function getDirector(movie) {  
  2.   if (movie.director != null) {  
  3.     return movie.director.name;  
  4.   }  
  5. }  
  6. getDirector(movieSmall); // => undefined  
  7. getDirector(movieFull);  // => 'Ridley Scott' 

if (movie.director) {...} 条件用于验证 director 属性是否已定义。如果没有这个预防措施,在访问movieSmall 对象 director 的时候,JavaScript 会抛出错误 TypeError: Cannot read property 'name' of undefined。

这是使用新的 optional chaining 功能的正确位置,并删除 movie.director 的存在验证。新版本的getDirector()看起来要短得多: 

  1. function getDirector(movie) {  
  2.   return movie.director?.name;  
  3. }  
  4. getDirector(movieSmall); // => undefined  
  5. getDirector(movieFull);  // => 'Ridley Scott' 

在表达式 movie.director?.name 中你可以找到 ?.: optional chaining 运算符。

在 movieSmall 的情况下,如果属性 director 丢失了。那么 movie.director?.name 的计算结果为 undefined。 optional chaining 运算符可防止抛出 TypeError:Cannot read property 'name' of undefined。

相反,在 movieFull 的情况下,属性 director 可用。 movie.director?.name 的值为 'Ridley Scott'.。

简单来说,代码片段: 

  1. let name = movie.director?.name; 

相当于: 

  1. let name;  
  2. if (movie.director != null) {  
  3.   name = movie.director.name;  

?. 通过减少 2 行代码简化了 getDirector() 函数。这就是我喜欢 optional chaining 的原因。

2.1 数组项

但是 optional chaining 功能可以做更多的事情。你可以在同一表达式中使用多个optional chaining 运算符。甚至可以使用它来安全地访问数组项目!

接下来的任务是编写一个返回电影主角名字的函数。

在 movie 对象中,actors 数组可以为空甚至丢失,因此你必须添加其他条件: 

  1. function getLeadingActor(movie) {  
  2.   if (movie.actors && movie.actors.length > 0) {  
  3.     return movie.actors[0].name;  
  4.   }  
  5. }  
  6. getLeadingActor(movieSmall); // => undefined  
  7. getLeadingActor(movieFull);  // => 'Harrison Ford' 

if (movie.actors && movies.actors.length > 0) {...} 条件需要确保 movie 中包含 actors 属性,并且此属性至少有一个 actor。

通过使用 optional chaining,此任务很容易解决: 

  1. function getLeadingActor(movie) {  
  2.   return movie.actors?.[0]?.name;  
  3. }  
  4. getLeadingActor(movieSmall); // => undefined  
  5. getLeadingActor(movieFull);  // => 'Harrison Ford' 

actors?. 确保 actors 属性存在。 [0]?. 确保第一个 actor 存在于列表中。很好!

3. nullish 合并

名为 nullish coalescing operator 的新提案建议用 ?? 处理 undefined或null,将它们默认为特定的值。

如果 variable 是undefined或null,则表达式 variable ?? defaultValue 的结果为defaultValue, 否则表达式的值为variable 的值。 

  1. const noValue = undefined;  
  2. const value = 'Hello';  
  3. noValue ?? 'Nothing'; // => 'Nothing'  
  4. value   ?? 'Nothing'; // => 'Hello' 

当评估为 undefined 时,Nullish 合并可以通过默认值来改进 optional chaining。

例如,当 movie 对象中没有 actor时,让我们改变 getLeading() 函数返回 "Unknown actor": 

  1. function getLeadingActor(movie) {  
  2.   return movie.actors?.[0]?.name ?? 'Unknown actor';  
  3. }  
  4. getLeadingActor(movieSmall); // => 'Unknown actor'  
  5. getLeadingActor(movieFull);  // => 'Harrison Ford' 

4. optional chaining 的 3 种形式

可以用以下 3 种形式使用 optional chaining 。

第一种形式 object?.property 用于访问静态属性: 

  1. const object = null;  
  2. object?.property; // => undefined 

第二种形式 object?.[expression] 用于访问动态属性或数组项: 

  1. const object = null;  
  2. const name = 'property';  
  3. object?.[name]; // => undefined  
  4. const array = null;  
  5. array?.[0]; // => undefined 

最后,第三种形式 object?.([arg1,[arg2,...]]) 执行一个对象方法: 

  1. const object = null;  
  2. object?.method('Some value'); // => undefined 

如果需要,可以通过组合这些表单来创建长的可选链: 

  1. const value = object.maybeUndefinedProp?.maybeNull()?.[propName]; 

5. 短路:停止于 null/undefined

有关 optional chaining 运算符的有趣之处在于,只要在其左侧 leftHandSide?.rightHandSide 中遇到无效值,右侧访问器的评估就会停止。这称为短路。

我们来看一个例子: 

  1. const nothing = null;  
  2. let index = 0;  
  3. nothing?.[index++]; // => undefined  
  4. index;              // => 0 

nothing 保持一个 nullish 值,因此 optional chaining 评估为 undefined ,并跳过右侧访问器的评估。因为 index 编号不会增加。

6. 何时使用 optional chaining

一定要克制使用 optional chaining 操作符访问任何类型属性的冲动:这将会导致误导使用。下一节将介绍何时正确使用它。

6.1 访问可能无效的属性

?. 必须只在可能无效的属性附近使用:maybeNullish?.prop。在其他情况下,使用旧的属性访问器:.property 或 [propExpression]。

回想一下 movie 对象。查看表达式 movie.director?.name,因 为director 可以是 undefined,在director属性附近使用 optional chaining 运算符是正确的。

相反,使用 ?. 来访问电影标题是没有意义的:movie?.title。movie 对象不会是无效的。 

  1. // Good  
  2. function logMovie(movie) {  
  3.   console.log(movie.director?.name);  
  4.   console.log(movie.title);  
  5. }   
  6. // Bad  
  7. function logMovie(movie) {  
  8.   // director needs optional chaining  
  9.   console.log(movie.director.name);  
  10.   // movie doesn't need optional chaining  
  11.   console.log(movie?.title);  

6.2 通常有更好的选择

以下函数 hasPadding() 接受带有可选 padding 属性的样式对象。 padding 具有可选属性left、top、right、bottom。

下面尝试使用 optional chaining 运算符: 

  1. function hasPadding({ padding }) {  
  2.   const top = padding?.top ?? 0;  
  3.   const right = padding?.right ?? 0;  
  4.   const bottom = padding?.bottom ?? 0;  
  5.   const left = padding?.left ?? 0;  
  6.   return left + top + right + bottom !== 0;  
  7. }  
  8. hasPadding({ color: 'black' });        // => false  
  9. hasPadding({ padding: { left: 0 } });  // => false  
  10. hasPadding({ padding: { right: 10 }}); // => true 

虽然函数正确地确定元素是否具有填充,但是对于每个属性都使用 optional chaining 是非常困难的。

更好的方法是使用对象扩展运算符将填充对象默认为零值: 

  1. function hasPadding({ padding }) {  
  2.   const p = {  
  3.     top: 0,  
  4.     right: 0,  
  5.     bottom: 0,  
  6.     left: 0,  
  7.     ...padding  
  8.   };  
  9.   return p.top + p.left + p.right + p.bottom !== 0;  
  10. }  
  11. hasPadding({ color: 'black' });        // => false  
  12. hasPadding({ padding: { left: 0 } });  // => false  
  13. hasPadding({ padding: { right: 10 }}); // => true 

在我看来,这个版本的 hasPadding() 更容易阅读。

7. 为什么我喜欢它?

我喜欢 optional chaining 运算符,因为它允许从嵌套对象轻松访问属性。它可以减少通过编写样板文件来验证来自访问器链的每个属性访问器上无效值的工作。

当 optional chaining 与无效合并运算符组合时,你可以获得更好的结果,能够更轻松地处理默认值。

 

责任编辑:庞桂玉 来源: segmentfault
相关推荐

2019-10-23 15:53:16

JavaScript可选链对象

2012-04-04 22:07:12

Android

2023-09-14 08:00:00

基于主干的开发分支模型

2009-06-04 17:33:08

EJB 3.1EJB 3.0

2015-10-26 09:58:53

程序员主流

2017-11-30 15:25:04

EclipseGo项目

2020-07-28 10:45:51

数据库三范式MySQL

2017-09-11 19:58:06

PostgreSQLMySQL数据库

2021-04-18 12:37:46

bspwmLinux窗口管理器

2018-01-09 18:46:44

数据库架构读写分离

2018-01-15 05:54:45

数据库读写分离互联网

2023-01-10 08:17:41

WebAPI框架

2019-11-18 09:56:48

谷歌Go语言开发者

2012-05-14 08:55:23

Android

2022-12-27 09:50:26

数据库方式

2020-12-20 17:34:50

Linux命令行终端

2023-07-04 16:28:23

2024-03-01 16:36:12

2012-02-28 09:11:51

语言Lua
点赞
收藏

51CTO技术栈公众号