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

鞏固一下 JS 可選 (?.)操作符號(hào),原來(lái)函數(shù)也可以用可選寫(xiě)法,又學(xué)到了!

開(kāi)發(fā) 前端
如何使用null (null和undefined)檢查訪問(wèn)對(duì)象的嵌套屬性?假設(shè)我們必須從后臺(tái)的接口訪問(wèn)用戶(hù)詳細(xì)信息。

[[404867]]

可選的鏈接?.操作符用于使用隱式空檢查訪問(wèn)嵌套對(duì)象屬性。

概述

如何使用null (null和undefined)檢查訪問(wèn)對(duì)象的嵌套屬性?假設(shè)我們必須從后臺(tái)的接口訪問(wèn)用戶(hù)詳細(xì)信息。

可以使用嵌套的三元運(yùn)算符 :

  1. const userName = response ? (response.data ? (response.data.user ? response.data.user.name : null) : null) : null

或者使用 if 進(jìn)行空值檢查:

  1. let userName = null
  2. if(response && response.data && response.data.user){ 
  3.   userName = response.data.user.name

或者更好的方法是使它成為一個(gè)單行鏈接的&&條件,像這樣:

  1. const userName = response && response.data && response.data.user && response.data.user.name

上述代碼的共同之處在于,鏈接有時(shí)會(huì)非常冗長(zhǎng),并且變得更難格式化和閱讀。這就是 ?.操作符被提出來(lái)的原因,我們改下 ?. 重構(gòu)上面的代碼:

  1. const userName = response?.data?.user?.name

很 nice 呀。

語(yǔ)法

?. 語(yǔ)法在ES2020 中被引入,用法如下:

  1. obj.val?.pro  // 如果`val`存在,則返回`obj.val.prop`,否則返回 `undefined`。 
  2.  
  3. obj.func?.(args) // 如果 obj.func 存在,則返回 `obj.func?.(args)`,否則返回 `undefined`。 
  4.  
  5. obj.arr?.[index] // 如果 obj.arr 存在,則返回 `obj.arr?.[index]`,否則返回 `undefined`。 

使用?.操作符

假設(shè)我們有一個(gè) user 對(duì)象:

  1. const user = { 
  2.   name"前端小智"
  3.   age: 21, 
  4.   homeaddress: { 
  5.     country: "中國(guó)" 
  6.   }, 
  7.   hobbies: [{name"敲代碼"}, {name"洗碗"}], 
  8.   getFirstName: function(){ 
  9.     return this.name
  10.   } 

屬性

訪問(wèn)存在的屬性:

  1. console.log(user.homeaddress.country);  
  2. // 中國(guó) 

訪問(wèn)不存在的屬性:

  1. console.log(user.officeaddress.country);  
  2. // throws error "Uncaught TypeError: Cannot read property 'country' of undefined" 

改用 ?. 訪問(wèn)不存在的屬性:

  1. console.log(user.officeaddress?.country);  
  2. // undefined 

方法

訪問(wèn)存在的方法:

  1. console.log(user.getFirstName());  
  2. // 前端小智 

訪問(wèn)不存在的方法:

  1. console.log(user.getLastName());  
  2. // throws error "Uncaught TypeError: user.getLastName is not a function"

改用 ?. 訪問(wèn)不存在的方法:

  1. console.log(user.getLastName?.());  
  2. // "undefined" 

數(shù)組

訪問(wèn)存在的數(shù)組:

  1. console.log(user.hobbies[0].name);  
  2. // "敲代碼" 

訪問(wèn)不存在的方法:

  1. console.log(user.hobbies[3].name);  
  2. // throws error "Uncaught TypeError: Cannot read property 'name' of undefined" 

改用 ?. 訪問(wèn)不存在的數(shù)組:

  1. console.log(user.dislikes?.[0]?.name);  
  2. // "undefined" 

?? 操作符

我們知道 ?. 操作符號(hào)如果對(duì)象不存在,剛返回 undefined,開(kāi)發(fā)中可能不返回 undefined 而是返回一個(gè)默認(rèn)值,這時(shí)我們可以使用雙問(wèn)題 ?? 操作符。

有點(diǎn)抽象,直接來(lái)一個(gè)例子:

  1. const country = user.officeaddress?.country; 
  2. console.log(country); 
  3. // undefined 

需要返回默認(rèn)值:

  1. const country = user.officeaddress?.country ?? "中國(guó)"
  2. console.log(country); 
  3. // 中國(guó) 

~完,我是刷碗智,SPA走起來(lái),下期見(jiàn)!

作者:Ashish Lahoti 譯者:前端小智 來(lái)源:CSS-Tricket

原文:https://codingncoepts.com/javascript/optional-chaining-opeator-javascript/

本文轉(zhuǎn)載自微信公眾號(hào)「大遷世界」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系大遷世界公眾號(hào)。

 

責(zé)任編輯:武曉燕
相關(guān)推薦

2021-05-20 10:22:34

操作符可選鏈操作符編程技巧

2024-01-16 07:33:02

SwiftTypeScript可選綁定

2022-09-19 08:48:03

項(xiàng)目初始化線程

2024-02-06 09:30:25

Figma矩形矩形物理屬性

2024-04-30 08:22:51

Figma圖形編輯變換矩陣

2021-11-16 12:25:14

jsPPT前端

2020-10-09 10:45:22

語(yǔ)言代碼數(shù)組

2015-09-02 10:33:54

紅包類(lèi)型optionals

2009-05-11 15:49:02

LinuxUbuntu戴爾

2011-01-21 13:56:44

SendmailSolaris

2021-12-27 08:45:19

固態(tài)硬盤(pán)硬盤(pán)存儲(chǔ)

2010-01-26 14:53:12

2023-07-17 09:19:20

CSSCSS 漸變

2021-01-20 06:29:42

JS工具操作符

2024-04-02 09:42:39

2009-02-19 13:28:08

遠(yuǎn)程通訊技術(shù)及原理Java

2019-10-23 15:53:16

JavaScript可選鏈對(duì)象

2024-01-17 06:23:35

SwiftTypeScript定義函數(shù)

2009-10-28 13:23:52

VB.NET可選參數(shù)

2013-04-19 11:24:19

Ubuntu 13.0GNOME桌面
點(diǎn)贊
收藏

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