前端开发,在接口约定基础上,必须对对用的对象做非空校验,来保证前端代码的健壮性
1. TypeScript 3.7 Optional Chaining
const data = {product:{productId:12},order:null}
const productId = data?.product?.productId //12
const orderId = data?.order?.orderId //Undefined
2.Lodash
const data = {product:{productId:12},order:null}
const productId = _.get(data,"product.productId") //12
const orderId = _.get(data,"order.orderId") //Undefined
optional chaining 的应用场景
obj?.prop // optional static property access
obj?.[expr] // optional dynamic property access
func?.(...args) // optional function or method call