7***@qq.com
7***@qq.com
  • 发布:2023-10-11 09:34
  • 更新:2023-10-11 16:42
  • 阅读:207

想问下这个是什么语法

分类:HBuilderX

order_price() {
let fee = this.orderinfo?.total_price ?? 0;
let money = this.coupon?.money ?? 0
return (Number(fee) - Number(money)).toFixed(2)
}

2023-10-11 09:34 负责人:无 分享
已邀请:
w***@163.com

w***@163.com - 前端菜鸡

this.orderinfo?.total_price:这部分代码首先检查 this.orderinfo 对象是否存在,如果存在,它会尝试获取 total_price 属性的值。如果 this.orderinfo 存在且包含 total_price 属性,那么 fee 将被赋值为 total_price 的值;如果 this.orderinfo 不存在或者 total_price 属性不存在,fee 将被赋值为 undefined。

  • w***@163.com

    ?? 0:最后,使用空值合并操作符 (??) 来处理可能的 undefined 或 null 值。如果 fee 或 money 的值为 undefined 或 null,那么它们将被赋值为 0。这确保了即使 total_price 和 money 不存在或为空,fee 和 money 也将有默认值 0。

    2023-10-11 16:42

爱豆豆

爱豆豆 - 办法总比困难多

这不是ts的写法吗

赵永强

赵永强 - 前端老菜鸟

es6的语法啊,??相当于是否undefined和null如果是,执行后面的,?.是先判断这个对象的属性是否存在

要回复问题请先登录注册