理解If语句中的Javascript对象

理解If语句中的Javascript对象,javascript,Javascript,我正试图修改此javascript以检索位置信息,但我无法确定下面示例中if语句的计算结果: 我不明白正在评估的具体if语句是: if (componentForm[addressType]) { var val = place.address_components[i][componentForm[addressType]]; document.getElementById(addressType).value = val; } 这将检查compo

我正试图修改此javascript以检索位置信息,但我无法确定下面示例中if语句的计算结果:

我不明白正在评估的具体if语句是:

if (componentForm[addressType]) {
        var val = place.address_components[i][componentForm[addressType]];
        document.getElementById(addressType).value = val;
      }

这将检查
componentForm[addressType]
是否未定义null空字符串0NaNfalse

这将检查
componentForm[addressType]
是否未定义null空字符串0NaNfalse

组件表单
是在
块顶部生成的对象:

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对象中的属性,您可以在需要动态方式访问属性时使用数组表示法-

if
语句正在测试componentForm对象
hasOwnProperty()
或其键名等于
addressType
中字符串值的任何
原型是否为真,然后使用
componentForm
中的结果值作为属性访问器,将该值获取到
place.address\u components[i]
对象中

place.address_components[i][componentForm[addressType]]
放置
-对象
address\u components
-array
i
-integer
componentForm
-object
addressType
-string

componentForm
是在
块顶部生成的对象:

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对象中的属性,您可以在需要动态方式访问属性时使用数组表示法-

if
语句正在测试componentForm对象
hasOwnProperty()
或其键名等于
addressType
中字符串值的任何
原型是否为真,然后使用
componentForm
中的结果值作为属性访问器,将该值获取到
place.address\u components[i]
对象中

place.address_components[i][componentForm[addressType]]
放置
-对象
address\u components
-数组
i
-整数
componentForm
-对象
addressType
-字符串如果(componentForm[addressType]){

计算是否存在
componentForm[addressType]

如果
条件失败

        0
        null
        ""
        ''
       [].length
       {}.length
       undefined
       false
以下是一些
如果
条件成功

"string"
"0"
" "
' '
[]
{}
true
您可以
console.log(componentForm[addressType])
alert(componentForm[addressType])
并检查上面提到的哪些场景是匹配的。

if(componentForm[addressType]){

计算是否存在
componentForm[addressType]

如果
条件失败

        0
        null
        ""
        ''
       [].length
       {}.length
       undefined
       false
以下是一些
如果
条件成功

"string"
"0"
" "
' '
[]
{}
true

您可以
console.log(componentForm[addressType])
警报(componentForm[addressType])
并检查上面提到的场景中哪些是匹配的。

所有答案都是正确的。我的问题是理解代码的其余部分。根据我现在的理解,是检查place.address\u components的addressType的componentForm中是否有短名称或长名称。所有答案都是正确的。我的problem正在理解代码的剩余部分。根据我现在的理解,它正在检查place.address\u组件的addressType的componentForm中是否有短名称或长名称。