How to catch Uncaught typeerror: cannot read property 'datatable' of undefined. error

How to catch Uncaught typeerror: cannot read property 'datatable' of undefined. error

当我关闭 Google API 时,我收到 "Uncaught TypeError: Cannot read property 'DataTable' of undefined" 消息。

我想捕获这个未定义的错误并弹出一个 window 说 "Google has been disabled" 但我不确定什么类型的对象是未定义的。我目前正在检查 "DataTable" 对象,但我很确定这是错误的,因为现在无论如何都会弹出 window。

这是我的代码。

var data = new google.visualization.DataTable();
if(typeof DataTable == "undefined"){
window.alert("sometext");
} else {
data.addColumn('string', 'Name');
data.addColumn('string', 'Manager');
data.addColumn('string', 'ToolTip');
data.addColumn('string', 'Type');
data.addColumn('string', 'Code');
data.addColumn('string', 'Aggregation');
data.addColumn('string', 'Stage');

data.addRows([
<xsl:apply-templates select="/*" mode="render"/>
['1','2','3','4','5','6','7']
]);

data.removeRow(data.getNumberOfRows() - 1);

drawDebugFlow(data);
}

而不是:

if(typeof DataTable == "undefined"){

使用:

if(typeof google == "undefined" || typeof google.visualization == "undefined"){

错误是"Uncaught TypeError: Cannot read property 'DataTable' of undefined"而不是"Datatable is undefined"