一、指数运算符(**)
2 ** 2 // 4
2 ** 3 // 8
此运算符有个特点为右结合
,于常见运算符左结合有所不同
// 相当于 2 ** (3 ** 2)
2 ** 3 ** 2 // 512
二、链判断运算符(?.)
在链式调用的时候判断,左侧的对象是否为null或undefined。如果是的,就不再往下运算,而是返回undefined
iterator.return?.()
上面代码中,iterator.return
如果有定义,就会调用该方法,否则iterator.return
直接返回undefined
,不再执行?.
后面的部分。
三、Null 判断运算符(??)
读取对象属性的时候,如果某个属性的值是null或undefined,为它们指定默认值。
const headerText = response.settings.headerText ?? true;
上面代码中,默认值只有在左侧属性值为null
或undefined
时,headerText
等于true
。
这个运算符的一个目的,就是跟链判断运算符?.配合使用
const animationDuration = response.settings?.animationDuration ?? 300;
上面代码中,如果response.settings
是null
或undefined
,或者response.settings.animationDuration
是null
或undefined
,就会返回默认值300。