如何测试是否定义了response.location.id?

时间:2012-08-26 20:00:07

标签: javascript variables testing attributes defined

我如何测试是否:

response.location.id

已定义?

谢谢

2 个答案:

答案 0 :(得分:1)

if (response && response.location && typeof response.location.id !== "undefined"){

 // is defined

}

答案 1 :(得分:1)

typeof response.location.id !== "undefined"

如果您不能依赖于定义的其余部分,那么:

response && response.location && typeof response.location.id !== "undefined"

如果您可以依赖它们,并且不必担心nullfalse0作为定义的id的值,那么只需测试它直接(if(response.location.id))。

相关问题