admin管理员组

文章数量:1435808

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.

本文标签: JavaScript consolelog execution orderStack Overflow