JavaScript console.log execution order?

早过忘川 提交于 2019-12-12 16:03:50

问题


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 commenting 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));

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/12737826/javascript-console-log-execution-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!