Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7994813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:13:37+00:00 2026-06-04T14:13:37+00:00

I’ve run into a rather interesting problem. The code below generates an array that,

  • 0

I’ve run into a rather interesting problem. The code below generates an array that, when logged, gives the value [undefined × 1]. According to this answer, that means that the array has unitialized indexes–in this case, only one. However, since I’m only using array.push() to avoid exactly this sort of problem, I’m kind of lost. To the best of my knowledge only assigning an index at least one index greater than other initialized indexes could cause the problem. Anyhow, here’s the code:

function Thing(params) {
    var that = this;

    this.OtherObject = function OtherObject(param) {
        this.param = param;
    }

    this.Foo = function Foo() {
        this.bar = [];

        this.add = function(item) {
            if (this.bar.length != 0) {
                for (var i = 0, len = this.bar.length; i < len; i++) {
                    if (this.bar[i].param <= item.param) {
                        this.bar.splice(i, 0, item);
                    }
                }
            } else {
                this.bar[0] = item;
                console.log(this.bar);
            }
        }

        this.pop = function() {
            return this.bar.pop();
        }

        this.length = function() {
            return this.bar.length;
        }
    }

    this.someFunc = function() {
        var myThing = new that.Foo();
        myThing.add(new that.OtherObject(3));

        while (myThing.length() > 0) {
            var someVar = myThing.pop();
        }
    }
}

var myThing = new Thing({
    myParam: "someValue"
});

myThing.someFunc();
​

I created a test case to see if I could narrow the problem down, but I ended up re-typing 95% of my code until I ran into the issue again, which appears to be related with my use of myThing.pop(). You can find a fiddle here which demonstrates the issue in question.

So… any ideas as to what could be causing this? Is this actually a problem with the way I’ve nested my objects? (which I did following a pattern given to me in this answer to a previous question of mine).

In case you’re wondering what I could possibly be trying to do here, I’m treating the outside object like a sort of namespace, which I’m doing to avoid cluttering up the global namespace (this will eventually be a pathfinding library, if I get my act together here). The Foo object is meant to be a sort of ordered list such that I can get the closest node (the one with the lowest param property) by pop()ing the stack.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:13:38+00:00Added an answer on June 4, 2026 at 2:13 pm

    There is just something going on that is very strange with Google Chrome’s console.log function. Other browsers seem to behave as you would expect. This is one one of the weirder things I’ve seen: if you add a call to alert after the lne that calls console.log, there will be different output in the console.

    console.log(this.bar);
    alert(this.bar);
    
    // [> OtherObject]
    

    If you remove the alert, as you have seen, the console will output

    // [undefined × 1] 
    

    This sort of thing is either due to some race condition or some side effect that the alert function is causing. Here’s my theory on what’s going on behind the scenes:

    • The console.log function does not actually log values in the console until there is some halt in the code, and then it logs all the queued messages. The queue contains references to the messages that are to be logged.
    • The first item in this.bar is destroyed when you pop the array. The logger, unless there is some interruption, will display the message after the referenced value is destroyed. Thus, it displays [undefined × 1]
    • Calling alert causes a halt in the code so that the console can log the message before the referenced value is erased.

    I’m still not completely sure if this is really what’s happening, but it seems to be somwhat reasonable. I’ll do some more testing to check if my theory is correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.