(兼容性有待确定)
ES2020 之后的空值处理语法,主要用来让代码更安全,更简洁的处理 null/undefined
??: 空值运算合并符a ?? b解释:如果 a 是 null 或者 undefined ,则返回 b,否则返回 a 本身 注意:它只在空值的情况下才触发,不会把 0、false、"" 当成空
const x = null ?? 'default' // -> 'default'
const y = undefined ?? 'default' // -> 'default'
const z = 0 ?? 66 // -> 0
const w = '' ?? 'aaa' // -> ''??= 逻辑空值赋值运算符x ??= y等价于: x = (x ?? y), 仅当 x 为 null 或者 undefined 的时候才赋值 y
注意:它只在空值的情况下才触发,不会把 0、false、"" 当成空
let a = null
let b = 0
let c
a ??= 10 // a = 10
b ??= 10 // b = 0
c ??= 10 // c = 10 因为 c 是 undefined toSorted :
Array 实例的 toSorted() 方法是 sort() 方法的复制方法版本。它不会改变原数组,会返回一个新数组,其元素按升序排列。
toReversed() :
Array 实例的 toReversed() 方法是 reverse() 方法对应的复制版本。它返回一个元素顺序相反的新数组。
toSpliced() :
Array 实例的 toSpliced() 方法是 splice() 方法的复制版本。它返回一个新数组,并在给定的索引处删除和/或替换了一些元素。
with() :
Array 实例的 with() 方法是使用方括号表示法修改指定索引值的复制方法版本。它会返回一个新数组,其指定索引处的值会被新值替换。
arrayInstance.with(index, value)
const arr = [1,2,3,4,5,6,7]
const a = arr.with(3, 999)
console.log(a) // [1, 2, 3, 999, 5, 6, 7]
console.log(arr) // [1, 2, 3, 4, 5, 6, 7]