0

我该如何测试:

response.location.id

被定义为 ?

谢谢

4

2 回答 2

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

 // is defined

}
于 2012-08-26T20:02:08.723 回答
1
typeof response.location.id !== "undefined"

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

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

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

于 2012-08-26T20:03:44.507 回答