javascript - Google Chrome 上的结果不可预测

标签 javascript google-chrome loops firefox object

我有一个名为history的javascript对象。在测试我的应用程序时,在 chrome 上得到完全不可预测的结果。

在 Firefox 上,我的应用程序运行良好:当我 console.log(history) 获取以下屏幕时

enter image description here

但在 Chrome 上受到关注

enter image description here

当我第一次看到这个屏幕时,我想也许 Chrome 添加了一些其他细节,例如长度和状态。但事实上,当我在 chrome 上迭代对象(如下面的示例)时,它首先打印出 state 并将值设置为 null。

$.each(history, function (hash,files) { ...

有趣的是,相同的代码在 Firefox 上运行起来就像一个魅力。

我做错了什么?

最佳答案

您不应定义名为 history 的变量,因为它已经是 Web API 接口(interface)的一部分,

which provides an interface for manipulating the browser session history (pages visited in the tab or frame that the current page is loaded in) - Window.history (Web API Interfaces | MDN)

它包含在window对象下,因此它是网页中的全局变量。您无法覆盖它,但这取决于浏览器,哪个浏览器优先于另一个。

关于javascript - Google Chrome 上的结果不可预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25068936/

相关文章:

javascript - javascript foreach 循环、for..in 和 Angular forEach 循环之间的区别?

javascript - Socket.io 在页面刷新时创建多个连接

Java 点列表

javascript - d3js 缩放事件处理程序未正确触发事件

javascript - 使用 Phonegap 为 iPhone 生成 PDF

javascript - 如何在 canplay 事件中设置 HTMLMediaElement 的 .currentTime,调用播放并避免调度暂停事件?

javascript - 我可以在任何站点中编辑文件并更新页面吗?

google-chrome - Chrome 扩展 - 每个页面加载多次注入(inject)内容脚本

r - 如何收到有关循环/应用程序正在进行的进度的警报

variables - 计算循环中的变量数量