28

谷歌分析显示,我们总用户中约 12% 受到以下 Javascript 错误的影响:

TypeError: 'undefined' is not an object

90% 的浏览器是 Safari 7534.48.3,10% 是 Mozilla 兼容代理。75% 的错误来自 iPhone,23% 来自 iPad。1% 来自 Macintosh,另外 2% 来自 iPod 等。这些设备都没有运行 Linux 或 Windows。

我曾尝试在 iPhone 和 iPad 上的 Safari 中启用调试模式,但无法重现该错误。

这是指向 Google Analytics 声称显示错误的页面的链接。如果有人可以在这里始终如一地重现错误,我将非常高兴,因为只需一个行号就足以让我开始调试。

谁能想到我可以尝试调试的任何其他方法?谢谢大家

对于我们中间的好奇,我正在使用此代码向 GA 发送错误-警告:可能的自我提升。

更新: TypeError:'undefined' 不是对象(评估 'safari.self.tab.canLoad')

单击时设法摆脱它,主要是在iphone上单击“更改国家..”

更新:通过确保元素在 dom 中可用来解决这个问题。原来成功的 ajax 调用试图写入一个不可用的元素。

我保留了Unable to replicate TypeError: 'undefined' is not an object here的可靠记录

4

2 回答 2

1

不明确的

未定义类型只有一个值:未定义。如果没有为它们分配值,则它是所有变量在声明后具有的默认值。你也可以将 undefined 的值赋给任何变量,但一般来说,应该避免这种情况,因为如果我们需要将一个变量标记为没有任何有意义的值,我们应该使用 null。

Let declaredVar;
console.log(typeof declaredVar); // -> undefined

declaredVar = 5;
console.log(typeof declaredVar); // -> number

declaredVar = undefined;
console.log(typeof declaredVar); // -> undefined

The undefined value can also be returned by the typeof operator when a non-existent variable is used as an argument.

Console.log(typeof notDeclaredVar); // -> undefined
console.log(notDeclaredVar); // -> Uncaught ReferenceError: notDeclared is not defined
于 2021-12-19T18:14:54.273 回答
-4

在你的functions.js中,你有这个:

storage_get = function(key) {
  var store = (window.SAFARI ? safari.extension.settings : localStorage);
  var json = store.getItem(key);
  if (json == null)
    return undefined;
  try {
    return JSON.parse(json);
  } catch (e) {
    log("Couldn't parse json for " + key);
    return undefined;
  }
}

undefined不是 JavaScript 关键字。这是一个(大多数情况下)碰巧未定义的变量。你不能这样使用undefined。考虑一下如果你用 替换它会发生什么pinkelephant,因为这正是这里发生的事情。

于 2012-04-09T18:33:51.203 回答