leetcode 2619

遇到了 ?? 语法糖,简而言之

  1. 可选链功能
    就是防止抛出异常
caught TypeError: Cannot read properties of undefined (reading 'salary')
    at <anonymous>:1:12

const user = {name:'11',age:'22'}
user.write.salary() == 抛出错误
user.write?.salary() == 显示undefined

  1. 空值凝聚
    const value1 = 0 || 'default string';
    console.log(value1);

const value2 = '' || 1000;
console.log(value2);

const value1 = 0 ?? 'default string';
console.log(value1);

const value2 = '' ?? 1000;
console.log(value2);

  1. 报错凝聚?
    return this[this.length - 1] ?? -1
    如果this.length == 0 的话原先会抛出错误现在不会了
posted on 2023-04-18 23:28  HDU李少帅  阅读(93)  评论(0编辑  收藏  举报