2015-08-30 105 views
0

你如何處理這種情況?使用Underscore檢查嵌套屬性有()

_.has(item.attributes.format,'dateUpdated') 

但是您會注意到有時候「格式」也不存在。你這樣檢查嗎

if (_.has(item.attributes,'format')) { 
    if (_.has(item.attributes.format,'dateUpdated')) { 
    // ok, I'm sure it exists 
    } 
} 

什麼是理想/專業/經驗豐富的程序員檢查它的方式?

+0

爲什麼不只是如果(obj.property && obj.property.nested){..code ...}? – DonSinDRom

回答