【发布时间】:2011-08-15 16:57:52
【问题描述】:
我的 IVR 应用以 JS 对象和数组的形式接收业务数据。例如,我们的一位客户的姓名访问如下:
customerData.customerList[customerIndex].customerName
现在,在某些情况下,customerName 未定义,因为整个对象未定义。现在,为了捕捉到这一点,我有一些嵌套逻辑检查每个级别是否未定义,然后最终检查最后一个:
if (typeof customerData != 'undefined' &&
typeof customerData.customerList &&
typeof customerData.customerList[customerIndex] != 'undefined' &&
typeof customerData.customerList[customerIndex].customerName != 'undefined')
{
//do something awesome with customer name, here
}
有没有更简单(更干净?)的方法来完成此操作,而无需检查对象上的每个字段?
谢谢。
【问题讨论】:
标签: javascript types typeof