最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

JavaScript console.log execution order? - Stack Overflow

matteradmin4PV0评论

I have code that looks something like this:

function pathfind (start,end,map)
{
    this.Init = function ()
    {
       this.open_node = new Array();
       this.open_node.push(start);
       console.log(this.open_node);
       this.Loop();
    }
    this.Loop = function ()
    {
       //Some code here
    }
    this.Init();
}

For some reason when I push "start" to this.open_node and I log its value, I get "undefined". However, after some bug testing I realized that menting out this.Loop(); in this.Init causes push to function properly and console.log to return [start] as it should. Can anyone explain why on earth this behavior would occur?

EDIT: I'm calling

pathfind({x:2,y:2},{x:24,y:24},parsemap(25,25));

I have code that looks something like this:

function pathfind (start,end,map)
{
    this.Init = function ()
    {
       this.open_node = new Array();
       this.open_node.push(start);
       console.log(this.open_node);
       this.Loop();
    }
    this.Loop = function ()
    {
       //Some code here
    }
    this.Init();
}

For some reason when I push "start" to this.open_node and I log its value, I get "undefined". However, after some bug testing I realized that menting out this.Loop(); in this.Init causes push to function properly and console.log to return [start] as it should. Can anyone explain why on earth this behavior would occur?

EDIT: I'm calling

pathfind({x:2,y:2},{x:24,y:24},parsemap(25,25));
Share Improve this question edited Oct 5, 2012 at 0:38 Jack Guy asked Oct 5, 2012 at 0:18 Jack GuyJack Guy 8,5338 gold badges60 silver badges88 bronze badges 4
  • Where's the line that calls pathfind(). What you're passing may have an impact... – Lee Taylor Commented Oct 5, 2012 at 0:21
  • Thanks for pointing that out. My testing showed that data types didn't matter, but here you go. – Jack Guy Commented Oct 5, 2012 at 0:22
  • what happens if you put a breakpoint just before console.log statement ? – sbr Commented Oct 5, 2012 at 1:24
  • No effect. I should have realized the Chrome console is asynchronous so wouldn't play nice. – Jack Guy Commented Oct 5, 2012 at 22:50
Add a ment  | 

2 Answers 2

Reset to default 6

After further research I found that console.log doesn't execute immediately in Chrome. Hence the outdated reports.

Your code executes pathfind function that returns undefined(and it should be this way) but you wait for result from this.Init function. Should probably execute it instead of pathfind.

Post a comment

comment list (0)

  1. No comments so far