let x = foo?.bar.baz();
等效于:
let x = (foo === null || foo === undefined) ? undefined : foo.bar.baz();
注意:当baz为undefined时,仍然会报错,这时需要再次判断baz是否为空:
baz
undefined
let x = foo?.bar?.baz?.call(null) // 这里我使用了call来调用方法
Last updated 2 years ago