简体   繁体   English

我的JavaScript对象有一个长度字段,我没有分配

[英]My JavaScript object has a length field which I didn't assign

I have a rather strange problem which I haven't been able to find a solution to. 我有一个相当奇怪的问题,但我一直无法找到解决方案。 It might be because no one has had this issue, or it could be the fact that I'm rather lost on what to Google. 可能是因为没有人遇到过这个问题,或者可能是我对Google所迷茫的事实。

Here's what's going on. 这是怎么回事。 I'm building a Rock-Paper-Scissors machine learning demo, and I have two JavaScript objects which are important to the script. 我正在构建一个Rock-Paper-Scissors机器学习演示,并且我有两个对脚本很重要的JavaScript对象。 Those two objects are the winners (an object which stores what beats what), and history (keeps track of all the plays). 这两个对象是winners (一个存储什么胜过一个对象的对象)和history (保持所有播放的轨迹)。

At the very top of my script, I make both of those objects, as such: 在脚本的最顶部,我创建了两个对象,如下所示:

var winners = {

}

var history = {

}

Now I'm getting to the problem. 现在我要解决这个问题。 I have the rest of the code done, but it is irrelevant, because when I comment it out, and since I'm using p5.js, I have a preload function where I simply print out both objects. 我已经完成了其余的代码,但这无关紧要,因为当我注释掉它时,并且由于我使用的是p5.js,所以我有一个预加载函数,可以在其中简单地打印出两个对象。

function preload(){
    console.log(winners);
    console.log(history);
}

Then I go into Chrome and run the html which uses the script, open the console. 然后,我进入Chrome并运行使用该脚本的html,打开控制台。 I should see: 我应该看到:

Object {}
Object {}

But NOPE! 但是没有!

Object {}
History {length: 1, scrollRestoration: "auto", state: null}

The winners object is just an empty object, but my history object has a length attribute as well as some other stuff. 获胜者对象只是一个空对象,但我的历史记录对象具有length属性以及其他一些东西。 All of my other code is commented out, I promise. 我保证,我所有其他代码都已被注释掉。

Why does my history object have the stuff in it and how can I get rid of it? 为什么我的历史记录对象包含其中的内容?如何清除它?

在此处输入图片说明

The browser contains a window.history object, which is what you are seeing in the console log. 浏览器包含window.history对象,即您在控制台日志中看到的对象。 See more information about this object here: https://developer.mozilla.org/en-US/docs/Web/API/Window/history 在此处查看有关此对象的更多信息: https : //developer.mozilla.org/en-US/docs/Web/API/Window/history

To circumvent this problem, use a slightly different name for your history object. 为避免此问题,请为您的历史记录对象使用稍微不同的名称。

There is already a built-in object called history in the browser. 浏览器中已经有一个内置的对象,称为history

You can read more about it in: 您可以在以下网站中了解更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM