理解If语句中的Javascript对象
我正试图修改此javascript以检索位置信息,但我无法确定下面示例中if语句的计算结果: 我不明白正在评估的具体if语句是:理解If语句中的Javascript对象,javascript,Javascript,我正试图修改此javascript以检索位置信息,但我无法确定下面示例中if语句的计算结果: 我不明白正在评估的具体if语句是: if (componentForm[addressType]) { var val = place.address_components[i][componentForm[addressType]]; document.getElementById(addressType).value = val; } 这将检查compo
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
这将检查
componentForm[addressType]
是否未定义,null,空字符串,0,NaN或false这将检查componentForm[addressType]
是否未定义,null,空字符串,0,NaN或false组件表单
是在
块顶部生成的对象:
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
-arrayi
-integercomponentForm
-objectaddressType
-stringcomponentForm
是在块顶部生成的对象:
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中是否有短名称或长名称。