的链接.操作符用于使用隐式空检查访问嵌套对象属性

  可选的链接?.操作符用于使用隐式空检查访问嵌套对象属性。

  概述

  如何使用null (null和undefined)检查访问对象的嵌套属性?假设我们必须从后台的接口访问用户详细信息。

  可以使用嵌套的三元运算符 :

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">const userName = response ? (response.data ? (response.data.user ? response.data.user.name : null) : null) : null; </pre>

  或者使用 if 进行空值检查:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">let userName = null; if(response && response.data && response.data.user){   userName = response.data.user.name; } </pre>

  或者更好的方法是使它成为一个单行链接的&&条件,像这样:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">const userName = response && response.data && response.data.user && response.data.user.name; </pre>

  上述代码的共同之处在于js跳转代码,链接有时会非常冗长,并且变得更难格式化和阅读。这就是 ?.操作符被提出来的原因,我们改下 ?. 重构上面的代码:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">const userName = response?.data?.user?.name; </pre>

  很 nice 呀。

  语法

  ?. 语法在ES2020 中被引入,用法如下:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">obj.val?.pro  // 如果val存在,则返回obj.val.prop,否则返回 undefined。 obj.func?.(args) // 如果 obj.func 存在,则返回 obj.func?.(args),否则返回 undefined。 obj.arr?.[index] // 如果 obj.arr 存在,则返回 obj.arr?.[index],否则返回 undefined</pre>

  使用?.操作符

  假设我们有一个 user 对象:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">const user = {   name: "前端小智",   age: 21,   homeaddress: {     country: "中国"   },   hobbies: [{name: "敲代码"}, {name: "洗碗"}],   getFirstName: function(){     return this.name;   } } </pre>

  属性

  访问存在的属性:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">console.log(user.homeaddress.country);  // 中国 </pre>

  访问不存在的属性:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">console.log(user.officeaddress.country);  // throws error "Uncaught TypeError: Cannot read property 'country' of undefined" </pre>

  改用 ?. 访问不存在的属性:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">console.log(user.officeaddress?.country);  // undefined </pre>

  方法

  访问存在的方法:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">console.log(user.getFirstName());  // 前端小智 </pre>

  访问不存在的方法:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">console.log(user.getLastName());  // throws error "Uncaught TypeError: user.getLastName is not a function"; </pre>

  改用 ?. 访问不存在的方法:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">console.log(user.getLastName?.());  // "undefined" </pre>

  数组

  访问存在的数组:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">console.log(user.hobbies[0].name);  // "敲代码" </pre>

  访问不存在的方法:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">console.log(user.hobbies[3].name);  // throws error "Uncaught TypeError: Cannot read property 'name' of undefined" </pre>

  改用 ?. 访问不存在的数组:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">console.log(user.dislikes?.[0]?.name);  // "undefined" </pre>

  我们知道 ?. 操作符号如果对象不存在,刚返回 undefined,开发中可能不返回 undefined 而是返回一个默认值,这时我们可以使用双问题 ?? 操作符。

  有点抽象js跳转代码,直接来一个例子:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">const country = user.officeaddress?.country; console.log(country); // undefined </pre>

  需要返回默认值:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">const country = user.officeaddress?.country ?? "中国"; console.log(country); // 中国 </pre>

  - EOF -

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1395
0 评论
343

发表评论

!