JavaScript 的特性极大地改变了你的编码方式。从 ES2015 开始,对我代码影响最多的功能是解构、箭头函数、类和模块系统。
截至 2019 年 8 月,一项新提案 optional chaining 达到了第3阶段,这将是一个很好的改进。Optional Chaining 改变了从深层对象结构访问属性的方式。
下面让我们来看看 optional chaining 是如何通过在深度访问可能缺少的属性时删除样板条件和变量来简化代码的。
1. 问题
由于 JavaScript 的动态特性,对象可以有区别很大的嵌套对象结构。
通常,你在以下情况下处理此类对象:
-
获取远程 JSON 数据
-
使用配置对象
- 具有 optional 属性
虽然这为对象提供了支持不同结构数据的灵活性,但是在访问这些对象的属性时会增加复杂性。
bigObject 在运行时可以有不同的属性集:
1// One version of bigObject
2const bigObject = {
3 // ...
4 prop1: {
5 //...
6 prop2: {
7 // ...
8 value: 'Some value'
9 }
10 }
11};
12
13// Other version of bigObject
14const bigObject = {
15 // ...
16 prop1: {
17 // Nothing here
18 }
19};
因此,你必须手动检查属性是否存在:
1// Later
2if (bigObject &&
3 bigObject.prop1 != null &&
4 bigObject.prop1.prop2 != null) {
5 let result = bigObject.prop1.prop2.value;
6}
这会产生很多样板代码。如果不需要写这些代码那就太好了。
让我们看看 optional chaining 如何解决这个问题,并减少样板条件。
2. 轻松的深入访问属性
让我们设计一个保存电影信息的对象。该对象包含一个 title 属性,以及可选的 director 和 actors。
movieSmall 对象只包含 title,而 movieFull 包含完整的属性集:
1const movieSmall = {
2 title: 'Heat'
3};
4
5const movieFull = {
6 title: 'Blade Runner',
7 director: { name: 'Ridley Scott' },
8 actors: [{ name: 'Harrison Ford' }, { name: 'Rutger Hauer' }]
9};
让我们写一个获取导演名字的函数。请记住,director 属性可能会不存在:
1function getDirector(movie) {
2 if (movie.director != null) {
3 return movie.director.name;
4 }
5}
6
7getDirector(movieSmall); // => undefined
8getDirector(movieFull); // => 'Ridley Scott'
if (movie.director) {...} 条件用于验证 director 属性是否已定义。如果没有这个预防措施,在访问movieSmall 对象 director 的时候,JavaScript 会抛出错误 TypeError: Cannot read property 'name' of undefined。
这是使用新的 optional chaining 功能的正确位置,并删除 movie.director 的存在验证。新版本的getDirector()看起来要短得多:
1function getDirector(movie) {
2 return movie.director?.name;
3}
4
5getDirector(movieSmall); // => undefined
6getDirector(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'.。
简单来说,代码片段:
1let name = movie.director?.name;
相当于:
1let name;
2if (movie.director != null) {
3 name = movie.director.name;
4}
?. 通过减少 2 行代码简化了 getDirector() 函数。这就是我喜欢 optional chaining 的原因。
2.1 数组项
但是 optional chaining 功能可以做更多的事情。你可以在同一表达式中使用多个optional chaining 运算符。甚至可以使用它来安全地访问数组项目!
接下来的任务是编写一个返回电影主角名字的函数。
在 movie 对象中,actors 数组可以为空甚至丢失,因此你必须添加其他条件:
1function getLeadingActor(movie) {
2 if (movie.actors && movie.actors.length > 0) {
3 return movie.actors[0].name;
4 }
5}
6
7getLeadingActor(movieSmall); // => undefined
8getLeadingActor(movieFull); // => 'Harrison Ford'
if (movie.actors && movies.actors.length > 0) {...} 条件需要确保 movie 中包含 actors 属性,并且此属性至少有一个 actor。
通过使用 optional chaining,此任务很容易解决:
1function getLeadingActor(movie) {
2 return movie.actors?.[0]?.name;
3}
4
5getLeadingActor(movieSmall); // => undefined
6getLeadingActor(movieFull); // => 'Harrison Ford'
actors?. 确保 actors 属性存在。[0]?. 确保第一个 actor 存在于列表中。很好!
3. nullish 合并
名为 nullish coalescing operator 的新提案建议用 ?? 处理 undefined或null,将它们默认为特定的值。
如果 variable 是undefined或null,则表达式 variable ?? defaultValue 的结果为defaultValue, 否则表达式的值为variable 的值。
1const noValue = undefined;
2const value = 'Hello';
3
4noValue ?? 'Nothing'; // => 'Nothing'
5value ?? 'Nothing'; // => 'Hello'
当评估为 undefined 时,Nullish 合并可以通过默认值来改进 optional chaining。
例如,当 movie 对象中没有 actor时,让我们改变 getLeading() 函数返回 "Unknown actor":
1function getLeadingActor(movie) {
2 return movie.actors?.[0]?.name ?? 'Unknown actor';
3}
4
5getLeadingActor(movieSmall); // => 'Unknown actor'
6getLeadingActor(movieFull); // => 'Harrison Ford'
4. optional chaining 的 3 种形式
可以用以下 3 种形式使用 optional chaining 。
第一种形式 object?.property 用于访问静态属性:
1const object = null;
2object?.property; // => undefined
第二种形式 object?.[expression] 用于访问动态属性或数组项:
1const object = null;
2const name = 'property';
3object?.[name]; // => undefined
4
5const array = null;
6array?.[0]; // => undefined
最后,第三种形式 object?.([arg1,[arg2,...]]) 执行一个对象方法:
1const object = null;
2object?.method('Some value'); // => undefined
如果需要,可以通过组合这些表单来创建长的可选链:
1const value = object.maybeUndefinedProp?.maybeNull()?.[propName];
5. 短路:停止于 null/undefined
有关 optional chaining 运算符的有趣之处在于,只要在其左侧 leftHandSide?.rightHandSide 中遇到无效值,右侧访问器的评估就会停止。这称为短路。
我们来看一个例子:
1const nothing = null;
2let index = 0;
3
4nothing?.[index++]; // => undefined
5index; // => 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
2function logMovie(movie) {
3 console.log(movie.director?.name);
4 console.log(movie.title);
5}
6
7// Bad
8function logMovie(movie) {
9 // director needs optional chaining
10 console.log(movie.director.name);
11
12 // movie doesn't need optional chaining
13 console.log(movie?.title);
14}
6.2 通常有更好的选择
以下函数 hasPadding() 接受带有可选 padding 属性的样式对象。padding 具有可选属性left、top、right、bottom。
下面尝试使用 optional chaining 运算符:
1function 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
9hasPadding({ color: 'black' }); // => false
10hasPadding({ padding: { left: 0 } }); // => false
11hasPadding({ padding: { right: 10 }}); // => true
虽然函数正确地确定元素是否具有填充,但是对于每个属性都使用 optional chaining 是非常困难的。
更好的方法是使用对象扩展运算符将填充对象默认为零值:
1function 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
12hasPadding({ color: 'black' }); // => false
13hasPadding({ padding: { left: 0 } }); // => false
14hasPadding({ padding: { right: 10 }}); // => true
在我看来,这个版本的 hasPadding() 更容易阅读。
7. 为什么我喜欢它?
我喜欢 optional chaining 运算符,因为它允许从嵌套对象轻松访问属性。它可以减少通过编写样板文件来验证来自访问器链的每个属性访问器上无效值的工作。
当 optional chaining 与无效合并运算符组合时,你可以获得更好的结果,能够更轻松地处理默认值。
原文:https://dmitripavlutin.com/javascript-optional-chaining/
精彩评论