tc39/proposal-optional-chaining

Optional Chaining Array and Three orders

Closed this issue · 2 comments

obj?.prop // 自判断静态属性访问
obj?.[expr] // 自判断动态访问
func?.(...args) // 自判断函数或方法调用

arr Arrary

error

arr?

arr Arrary

error

arr?[0]?

arr Arrary

error

(arr?[0]?.bol?)?"a":"b"
console.log(getFieldValue('msid')?[0]?.batchs)

Hi,

It is not clear what you are trying to say, but trying to guess:.

  • You cannot use arr?[0]. If you expect that arr is sometimes null or undefined,
    use arr?.[0]. If you know that arr is never null or undefined, use arr[0].
  • You cannot use foo?. If you are trying to test whether foo is neither null nor undefined,
    use foo != null.

@claudepache Good,Confirmed