You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
a?.b// undefined if `a` is null/undefined, `a.b` otherwise.a==null ? undefined : a.ba?.[x]// undefined if `a` is null/undefined, `a[x]` otherwise.a==null ? undefined : a[x]a?.b()// undefined if `a` is null/undefineda==null ? undefined : a.b()// throws a TypeError if `a.b` is not a function// otherwise, evaluates to `a.b()`a?.()// undefined if `a` is null/undefineda==null ? undefined : a()// throws a TypeError if `a` is neither null/undefined, nor a function// invokes the function `a` otherwise
开发中,链式取值是非常正常的操作,如:
但是对于这种操作报出类似于
Uncaught TypeError: Cannot read property 'goods' of undefined
这种错误也是再正常不过了,如果说是res数据是自己定义,那么可控性会大一些,但是如果这些数据来自于不同端(如前后端),那么这种数据对于我们来说我们都是不可控的,因此为了保证程序能够正常运行下去,我们需要对此校验:如果再精细一点,对于所有都进行校验的话,就会像这样:
不敢想象,如果数据的层级再深一点会怎样,这种实现实在是非常不优雅,那么如果优雅地来实现链式取值呢?
一、optional chaining
这是一个出于stage 2的ecma新语法,目前已经有了babel的插件 babel-plugin-transform-optional-chaining,这种语法在swift中有,可以看下官方给的实例
二、通过函数解析字符串
我们可以通过函数解析字符串来解决这个问题,这种实现就是lodash的
_.get
方法实现起来也非常简单,只是简单的字符串解析而已:
三、使用解构赋值
这个思路是来自github上 You-Dont-Need-Lodash-Underscore 这个仓库,看到这个的时候真的佩服
当然,这个时候为了保证不报uncaught Typeerror,我们仍然需要定义默认值, 就像这样, 貌似如果不加lint可读性堪忧
四、使用Proxy
这个是组内同事提到的,一个简单实现如下:
我们可以这样使用:
这差不多就是心中所谓的优雅了。
综上,在实际工作中,使用方法四会是最优雅,可读性也非常强,但考虑到浏览器的话,可能方法二会更加常用,当然,如果你所要取的值层级不是太深,你组内的同事要严格的lint,方法三也不失为一种好的选择。
The text was updated successfully, but these errors were encountered: