自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

為什么我喜歡JavaScript的Optional Chaining

開發(fā) 前端
下面讓我們來看看 optional chaining 是如何通過在深度訪問可能缺少的屬性時刪除樣板條件和變量來簡化代碼的。

 [[275113]]

JavaScript 的特性極大地改變了你的編碼方式。從 ES2015 開始,對我代碼影響最多的功能是解構(gòu)、箭頭函數(shù)、類和模塊系統(tǒng)。

截至 2019 年 8 月,一項新提案 optional chaining 達到了第3階段,這將是一個很好的改進。Optional Chaining 改變了從深層對象結(jié)構(gòu)訪問屬性的方式。

下面讓我們來看看 optional chaining 是如何通過在深度訪問可能缺少的屬性時刪除樣板條件和變量來簡化代碼的。

1. 問題

由于 JavaScript 的動態(tài)特性,對象可以有區(qū)別很大的嵌套對象結(jié)構(gòu)。

通常,你在以下情況下處理此類對象:

  •  獲取遠程 JSON 數(shù)據(jù)
  •  使用配置對象
  •  具有 optional 屬性

雖然這為對象提供了支持不同結(jié)構(gòu)數(shù)據(jù)的靈活性,但是在訪問這些對象的屬性時會增加復雜性。

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;  

這會產(chǎn)生很多樣板代碼。如果不需要寫這些代碼那就太好了。

讓我們看看 optional chaining 如何解決這個問題,并減少樣板條件。

2. 輕松的深入訪問屬性

讓我們設(shè)計一個保存電影信息的對象。該對象包含一個 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. }; 

讓我們寫一個獲取導演名字的函數(shù)。請記住,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 的計算結(jié)果為 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() 函數(shù)。這就是我喜歡 optional chaining 的原因。

2.1 數(shù)組項

但是 optional chaining 功能可以做更多的事情。你可以在同一表達式中使用多個optional chaining 運算符。甚至可以使用它來安全地訪問數(shù)組項目!

接下來的任務(wù)是編寫一個返回電影主角名字的函數(shù)。

在 movie 對象中,actors 數(shù)組可以為空甚至丟失,因此你必須添加其他條件: 

  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,此任務(wù)很容易解決: 

  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 的結(jié)果為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() 函數(shù)返回 "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 用于訪問靜態(tài)屬性: 

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

第二種形式 object?.[expression] 用于訪問動態(tài)屬性或數(shù)組項: 

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

最后,第三種形式 object?.([arg1,[arg2,...]]) 執(zhí)行一個對象方法: 

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

如果需要,可以通過組合這些表單來創(chuàng)建長的可選鏈: 

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

5. 短路:停止于 null/undefined

有關(guān) optional chaining 運算符的有趣之處在于,只要在其左側(cè) leftHandSide?.rightHandSide 中遇到無效值,右側(cè)訪問器的評估就會停止。這稱為短路。

我們來看一個例子: 

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

nothing 保持一個 nullish 值,因此 optional chaining 評估為 undefined ,并跳過右側(cè)訪問器的評估。因為 index 編號不會增加。

6. 何時使用 optional chaining

一定要克制使用 optional chaining 操作符訪問任何類型屬性的沖動:這將會導致誤導使用。下一節(jié)將介紹何時正確使用它。

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 通常有更好的選擇

以下函數(shù) 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 

雖然函數(shù)正確地確定元素是否具有填充,但是對于每個屬性都使用 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 與無效合并運算符組合時,你可以獲得更好的結(jié)果,能夠更輕松地處理默認值。

 

責任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2019-10-23 15:53:16

JavaScript可選鏈對象

2012-04-04 22:07:12

Android

2023-09-14 08:00:00

基于主干的開發(fā)分支模型

2009-06-04 17:33:08

EJB 3.1EJB 3.0

2025-01-15 09:06:58

CSSRegEx前端

2015-10-26 09:58:53

程序員主流

2017-11-30 15:25:04

EclipseGo項目

2017-09-11 19:58:06

PostgreSQLMySQL數(shù)據(jù)庫

2020-07-28 10:45:51

數(shù)據(jù)庫三范式MySQL

2021-04-18 12:37:46

bspwmLinux窗口管理器

2018-01-09 18:46:44

數(shù)據(jù)庫架構(gòu)讀寫分離

2018-01-15 05:54:45

數(shù)據(jù)庫讀寫分離互聯(lián)網(wǎng)

2023-01-10 08:17:41

WebAPI框架

2019-11-18 09:56:48

谷歌Go語言開發(fā)者

2012-05-14 08:55:23

Android

2020-12-20 17:34:50

Linux命令行終端

2022-12-27 09:50:26

數(shù)據(jù)庫方式

2023-07-04 16:28:23

2024-03-01 16:36:12

點贊
收藏

51CTO技術(shù)棧公眾號