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’的属性的值。”