我正在尝试修改此javascript以检索位置信息,但是在以下示例中,我无法弄清楚if语句正在评估什么:https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
我不理解所评估内容的具体if语句是:
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
答案 0 :(得分:2)
这将检查componentForm[addressType]
是否不是未定义,空,空字符串, 0 , NaN 或 false 。
答案 1 :(得分:1)
componentForm
是位于<script>
块顶部的对象:
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
要访问JS对象中的属性,需要动态访问属性时可以使用Array表示法-https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#Syntax
if
语句正在测试componentForm对象hasOwnProperty()
或键名等于prototype
中字符串值的任何addressType
,如果为true,则使用componentForm
中的结果值作为place.address_components[i]
对象中的属性访问器来获取该值。
place.address_components[i][componentForm[addressType]]
place
-对象
address_components
-数组i
-整数componentForm
-对象addressType
-字符串
答案 2 :(得分:1)
如果(componentForm [addressType]){
评估componentForm[addressType]
是否存在
以下是if
条件失败的一些可能性
0
null
""
''
[].length
{}.length
undefined
false
这里有一些if
条件成功的案例
"string"
"0"
" "
' '
[]
{}
true
您可以console.log(componentForm[addressType])
或alert(componentForm[addressType])
并检查上述哪种情况是匹配的。