[英]Uncaught TypeError: Cannot read property 'timing' of undefined
How do I properly do object detection for window.performance.timing
? 如何正确检测
window.performance.timing
对象?
An unknown version of Chrome being run by Googlebot is spawning the following error: 由Googlebot运行的未知版本的Chrome产生以下错误:
Uncaught TypeError: Cannot read property 'timing' of undefined
未捕获的TypeError:无法读取未定义的属性“ timing”
The only instances of window.performance.timing
are in the following snippets of code: window.performance.timing
的唯一实例在以下代码片段中:
else if (
window.performance!=undefined
&& window.performance.timing!=undefined
&& window.performance.timing.toJSON!=undefined) {/* etc */}
Obviously regardless of my efforts to do object detection Googlebot is somehow still spawning the error message. 显然,无论我为进行对象检测所做的任何努力,Googlebot仍会以某种方式生成错误消息。 I can not use
try
and catch
and I have zero instances in my JavaScript error log of this happening in any testable (eg Chrome itself) browser, only Googlebot. 我无法使用
try
and catch
并且我的JavaScript错误日志中有零个实例,这种情况发生在任何可测试的浏览器(例如Chrome本身)中,只有Googlebot。
Try this way. 尝试这种方式。
else if (
window.performance
&& window.performance.timing
&& window.performance.timing.toJSON) {/* etc */}
And as I know, you should use !== instead of != 据我所知,您应该使用!==而不是!=
If window.performance
is undefined the if statement will still check the other two expressions. 如果未定义
window.performance
则if语句仍将检查其他两个表达式。 If there is no window.performance
it can't check for window.performance.timing
. 如果没有
window.performance
,则无法检查window.performance.timing
。
At least that's what I am guessing. 至少那是我的猜测。 I would try to nest the if statements:
我会尝试嵌套if语句:
if(window.performance != undefined) {
if(window.performance.timing != undefined) {
if(window.performance.timing.toJSON != undefined) {
/* etc */
}
}
}
What if window.performance
is null
? 如果
window.performance
为null
怎么办? I bet you would get the same error message. 我敢打赌,您会收到相同的错误消息。
That's why the canonical way to check objects existence is: 这就是为什么检查对象存在的规范方法是:
if (window.performance && window.performance.whatever && ...) ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.