JS 特性:可选链(?.)

可选链操作符

参考文献:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Optional_chaining

https://blog.csdn.net/qq_45534034/article/details/104273131

什么是可选链

可选链操作符( ?. )允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效。?. 操作符的功能类似于 . 链式操作符,不同之处在于,在引用为空(nullish ) (null 或者 undefined) 的情况下不会引起错误,该表达式短路返回值是 undefined。与函数调用一起使用时,如果给定的函数不存在,则返回 undefined。

上述是官方描述,举个例子对象嵌套了好多层,需要获得对象深层的值得时候,这就意味着你需要写很长的属性访问,如下:

const person = {
    details: {
        name: {
            newName: "aa",
            oldName: "aa",
        }
        age: "18",
    },
    jobs: [
        "H5",
        "Java"
    ]
} 
const personNewName = person.details.name.newName;

上面的代码如果我的person.details.name等任何一层数据有问题或者不存在的时候,js就会报错,我们一般会这么改进:

 const personNewName =  person && person.details && person.details.name person.details.name.newName|| '';

可以看到为了访问某个人的 newName,代码变得非常不优雅,会有多个&&代码量也很大。可选链 就是为了解决这个问题而诞生的。

用法

有了可选链操作符(?.),在访问 person.details.name.newName 之前,不再需要明确地校验 person.details.name 的状态,再并用短路计算获取最终结果:

const personFirstName = person?.details?.name?.firstName;

其实就是在属性访问符 . 的前面加了个问号。我们看上面语句中第一个 ?. ,从 JS 层面,它表示如果 person 的值为 null 或者 undefined,就不会报错而返回 undefined,否则才继续访问后面的 details 属性。而如果后面的属性访问链中有任何一个属性为 null 或者 undefined,那么最终的值就为 undefined。
这等价于以下表达式,但实际上没有创建临时变量:

let temp = person.details;
let nestedProp = ((temp === null || temp === undefined) ? undefined : temp.name);
let temp1 = person.details.name;
let nestedProp = ((temp1 === null || temp1 === undefined) ? undefined : temp1.firstName);

可选链与函数调用

函数调用时如果被调用的方法不存在,使用可选链可以使表达式自动返回undefined而不是抛出一个异常。

let result = someInterface.customMethod?.();

注意: 如果存在一个属性名且不是函数, 使用 ?. 仍然会产生一个 TypeError 异常 (x.y is not a function).
注意: 如果 someInterface 自身是 null 或者 undefined ,异常 TypeError 仍会被抛出 someInterface is null 如果你希望允许 someInterface 也为 null 或者 undefined ,那么你需要像这样写 someInterface?.customMethod?.()

可选链和表达式

当使用方括号与属性名的形式来访问属性时,你也可以使用可选链操作符:

let nestedProp = obj?.['prop' + 'Name'];

可选链不能用于赋值

let object = {};
object?.property = 1; // Uncaught SyntaxError: Invalid left-hand side in assignment

可选链访问数组元素

let arrayItem = arr?.[42];

规范

在这里插入图片描述

浏览器兼容性

在这里插入图片描述
这个特性在很多其他的语言如 C#,Swift 中都有实现,并且 TypeScript 中也已经加入该特性。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
fieldList: this.$refs.tabTable.$refs.field ? this.$refs.tabTable.$refs.field[0].tableData : this.tableDataField || [], // 字段列表 conditionList: this.$refs.tabTable.$refs.sql ? this.$refs.tabTable.$refs.sql[0].tableData : this.tableDataSql || [], // 过滤条件 sequenceList: this.$refs.tabTable.$refs.sort ? this.$refs.tabTable.$refs.sort[0].tableData : this.tableDataSort || [], // 字段排序 queryList: this.$refs.tabTable.$refs.filter ? this.$refs.tabTable.$refs.filter[0].tableData : this.tableDataFilter || [], // 查询区 view_order: this.viewOrder, // 视图顺序 module_id: this.addEditParams.module_id, // 功能id status_id: this.addEditParams.status_id, // 状态id table_name: this.addEditParams.table_name, // 表名 bill_type_id: this.addEditParams.bill_type_id, // 单据类型id des_id: this.addEditParams.des_id ? this.addEditParams.des_id : '', // 视图模板id des_alias: this.addEditParams.des_alias ? this.addEditParams.des_alias : '', // 视图别名 resource_code: this.addEditParams.resource_code ? this.addEditParams.resource_code : '', // 资源编码 page_num: this.addEditParams.page_num ? this.addEditParams.page_num : 0, // 每页条数 is_subtotal: this.addEditParams.is_subtotal ? this.addEditParams.is_subtotal : 0, // 是否小计 }; objSub.fieldList.forEach((it) => { it.is_format = it.is_format ? 1 : 0; it.is_visible = it.is_visible ? 1 : 0; it.is_total_items = it.is_total_items ? 1 : 0; it.is_primary = it.is_primary ? 1 : 0; return it; }); 优化以上代码
07-14
要优化以上代码,可以使用数组的 `map` 方法来遍历并修改 `fieldList` 数组中的每个对象。同时,可以使用逻辑或运算符 `||` 来简化对可能为空的变量的赋值操作。以下是优化后的代码示例: ```javascript const fieldList = this.$refs.tabTable?.$refs.field?.[0]?.tableData || this.tableDataField || []; const conditionList = this.$refs.tabTable?.$refs.sql?.[0]?.tableData || this.tableDataSql || []; const sequenceList = this.$refs.tabTable?.$refs.sort?.[0]?.tableData || this.tableDataSort || []; const queryList = this.$refs.tabTable?.$refs.filter?.[0]?.tableData || this.tableDataFilter || []; const objSub = { fieldList: fieldList.map(it => ({ ...it, is_format: it.is_format ? 1 : 0, is_visible: it.is_visible ? 1 : 0, is_total_items: it.is_total_items ? 1 : 0, is_primary: it.is_primary ? 1 : 0 })), conditionList, sequenceList, queryList, view_order: this.viewOrder, module_id: this.addEditParams.module_id, status_id: this.addEditParams.status_id, table_name: this.addEditParams.table_name, bill_type_id: this.addEditParams.bill_type_id, des_id: this.addEditParams.des_id || '', des_alias: this.addEditParams.des_alias || '', resource_code: this.addEditParams.resource_code || '', page_num: this.addEditParams.page_num || 0, is_subtotal: this.addEditParams.is_subtotal || 0 }; // 打印优化后的对象 console.log(objSub); ``` 在优化后的代码中,我们使用了可链操作符 `?.` 来避免访问可能为空的变量时出现的错误。我们还使用了逻辑或运算符 `||` 来简化对可能为空的变量的赋值操作。对于 `fieldList` 数组,我们使用了 `map` 方法来遍历并修改每个对象的属性。最后,我们输出了优化后的 `objSub` 对象。 请注意,上述代码使用了 ES6+ 的语法,确保你的开发环境支持这些语法特性

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值