【问题标题】:Unable to reproduce TypeError: 'undefined' is not an object无法重现 TypeError:“未定义”不是对象
【发布时间】:2012-04-22 02:21:35
【问题描述】:

Google Analytics 显示,我们总用户中约 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 中启用调试模式,但无法重现该错误。

Here is a link to a page Google Analytics claims is showing the error。如果有人能在此处始终如一地重现该错误,我将非常高兴,因为只需一个行号就足以让我开始调试。

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

对于我们当中的好奇者,我正在使用this code to send errors to GA -- 警告:可能自我推销。

更新: TypeError: 'undefined' is not an object (evalating 'safari.self.tab.canLoad')

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

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

我一直保持Unable to reproduce TypeError: 'undefined' is not an object here的可靠记录

【问题讨论】:

  • 那么,出现错误的文件和行是什么?您的代码也将其发送给 GA。
  • undefined:0 .. :( -- 巧克力茶壶浮现在脑海中......
  • @JohnMcLear:如果您找到答案,我认为最好将其作为答案发布并批准(允许),而不是简单地更新您的问题,因为它看起来仍然有效(未回答)当它不是。
  • @JohnMcLear:在撰写此评论时,您最后一次出现是在 3 小时前。那么,您是否可以按照上述已约会近 3 年的建议并回答自己以将该问题标记为已回答?
  • 如果你想确定未定义的函数,这种类型不仅在接口的打字稿上找到,但如果问题和克隆,修改这样的函数。 TypeError: 'undefined' 是声明性的。 no 已在对象中转换。

标签: javascript jquery iphone safari


【解决方案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

【讨论】:

    【解决方案2】:

    在你的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 会发生什么,因为这正是这里发生的事情。

    【讨论】:

    • 这很奇怪,我没有看到functions.js...对你来说functions.js 是什么来源? IE 是来自 cloudflare、google 还是 sharethis?
    • 使用pinkelephant 会抛出一个ReferenceError,使用undefined 不会并且只是返回undefined 值。另外,我很确定您正在查看 Chrome AdBlock 扩展程序的代码:code.google.com/p/adblockforchrome/source/browse/trunk/… :)
    • 哈哈好点 :) 好吧,这是我能找到的唯一 undefined...也许这就是他的问题的答案 - 浏览器扩展有错误?
    • 你不只是用pinkelephant替换undefined,你声明了一个名为pinkelephant的未定义变量,例如“var pinkelephant;”然后不是做“return undefined;”那么你可以做“return pinkelephant;”
    • 这似乎不正确。根据w3schools.com/jsref/jsref_undefined.asp,所有主要浏览器都正确支持“未定义”之类的东西。
    猜你喜欢
    • 2014-07-12
    • 2021-06-11
    • 2016-07-03
    • 2018-10-24
    • 1970-01-01
    • 2018-12-21
    • 2021-12-17
    • 2021-12-01
    • 2019-11-16
    相关资源
    最近更新 更多