JavaScript console.log execution order? - Stack Overflow

I have code that looks something like this:function pathfind (start,end,map){this.Init = function (){t

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745641303a4637711.html

相关推荐

  • JavaScript console.log execution order? - Stack Overflow

    I have code that looks something like this:function pathfind (start,end,map){this.Init = function (){t

    22天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信