Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获的TypeError,即使在if子句中使用了typeof_Javascript - Fatal编程技术网

Javascript 未捕获的TypeError,即使在if子句中使用了typeof

Javascript 未捕获的TypeError,即使在if子句中使用了typeof,javascript,Javascript,为什么这仍然是真的,而且还会回来 $.each(res.data,function(idx,val){ if (typeof val.location.longitude != 'undefined') { console.log(val.name + ':' + val.location.latitude + ', ' + val.location.longitude); } 即使未定义经度?您也检查了错误的内容: Uncaught TypeError: Cannot read prope

为什么这仍然是真的,而且还会回来

$.each(res.data,function(idx,val){
if (typeof val.location.longitude != 'undefined') {
 console.log(val.name + ':' + val.location.latitude + ', ' + val.location.longitude);
}

即使未定义经度?

您也检查了错误的内容:

Uncaught TypeError: Cannot read property 'longitude' of undefined 

错误消息告诉您
val.location
undefined
,而不是“longitude”属性是
undefined
。它是,“您好,我无法从值为
未定义的对象中读取名为“longitude”的属性的值”,换句话说。

您检查的内容是错误的:

Uncaught TypeError: Cannot read property 'longitude' of undefined 
错误消息告诉您
val.location
undefined
,而不是“longitude”属性是
undefined
。换言之,“您好,我无法从值为
未定义的对象读取名为‘longitude’的属性的值。”