Daneel Rakow Daneel Rakow - 4 years ago 136
Javascript Question

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.

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. Those two objects are the

winners
(an object which stores what beats what), and
history
(keeps track of all the plays).

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.

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


Then I go into Chrome and run the html which uses the script, open the console. 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. 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?

enter image description here

Answer Source

The browser contains a window.history object, which is what you are seeing in the console log. See more information about this object here: https://developer.mozilla.org/en-US/docs/Web/API/Window/history

To circumvent this problem, use a slightly different name for your history object.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download